Four solutions track key duration, test arithmetic subarrays, minimize path effort, and transform matrix ranks by connectivity.
Rank
Name
Score
Finish Time
Q1 (3)
Q2 (4)
Q3 (5)
Q4 (6)
103 / 10984
YoungForest
19
1:19:51
0:06:36
0:12:02
0:39:44 2
1:09:51
This week’s weekly contest continued the strong momentum, and my ranking was also very high. Together with last week’s ranking, my rank in the Cruel group rose to a new high, 11th.
1629. Slowest Key
One pass. Use a variable to maintain the time of the previous key press.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: charslowestKey(vector<int>& releaseTimes, string keysPressed){ constint n = releaseTimes.size(); int last = 0; int maxDuration = 0; char ans = ' '; for (int i = 0; i < n; ++i) { constint duration = releaseTimes[i] - last; if (duration > maxDuration || (duration == maxDuration && keysPressed[i] > ans)) { maxDuration = duration; ans = keysPressed[i]; } last = releaseTimes[i]; } return ans; } };
Space complexity: O(1),
time complexity: O(n).
1630. Arithmetic Subarrays
For each subarray, sort it and check whether it forms an arithmetic sequence.
Time complexity: O(rows * cols),
space complexity: O(rows * cols).
1632. Rank Transform of a Matrix
Process numbers from small to large, handling equal numbers each time.
Equal numbers need to be checked for connectivity by row and column, using Union-Find. Numbers in the same connected component need to take the maximum rank; only then is the answer minimized.
classSolution { structUF { vector<int> parent; int count; UF(int n) : parent(n), count(n) { iota(parent.begin(), parent.end(), 0); } // A utility function to find the subset of an element i intfind(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }
// A utility function to do union of two subsets voidunite(int x, int y) { int xset = find(x); int yset = find(y); if(xset != yset) { parent[xset] = yset; --count; } } }; public: vector<vector<int>> matrixRankTransform(vector<vector<int>>& matrix) { constint m = matrix.size(); constint n = matrix[0].size(); vector<int> rowsRank(m, 0); vector<int> colsRank(n, 0); vector<vector<int>> ans(m, vector<int>(n, 0)); using pii = pair<int, int>; map<int, vector<pii>> arr; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { arr[matrix[i][j]].emplace_back(i, j); } } for (constauto & p : arr) { constauto & v = p.second; constint vsize = v.size(); UF uf(vsize); vector<int> rowsIndex(m, -1); vector<int> colsIndex(n, -1); for (int i = 0; i < vsize; ++i) { auto [x, y] = v[i]; if (rowsIndex[x] == -1 && colsIndex[y] == -1) { rowsIndex[x] = i; colsIndex[y] = i; } elseif (rowsIndex[x] == -1) { rowsIndex[x] = i; uf.unite(i, colsIndex[y]); } elseif (colsIndex[y] == -1) { colsIndex[y] = i; uf.unite(i, rowsIndex[x]); } else { uf.unite(i, rowsIndex[x]); uf.unite(i, colsIndex[y]); } } unordered_map<int, vector<int>> cluster; for (int i = 0; i < vsize; ++i) { cluster[uf.find(i)].push_back(i); } for (constauto& p : cluster) { constauto& vec = p.second; int maxRank = numeric_limits<int>::min(); for (int i : vec) { maxRank = max(rowsRank[v[i].first] + 1, maxRank); maxRank = max(colsRank[v[i].second] + 1, maxRank); } for (int i : vec) { rowsRank[v[i].first] = maxRank; colsRank[v[i].second] = maxRank; ans[v[i].first][v[i].second] = maxRank; } } } return ans; } };
Time complexity: O(m * n * log mn),
space complexity: O(m * n).