Biweekly notes on largest unique values, Armstrong numbers, minimum spanning trees and topological ordering, finished with time to spare.
Rank
Name
Score
Finish Time
Q1 (3)
Q2 (3)
Q3 (5)
Q4 (7)
98 / 1634
YoungForest
18
0:35:23
0:11:01
0:10:53
0:22:40
0:35:23
It had been two months since I last participated in a biweekly contest, and the number jumped directly from 1 to 5.
This contest was very simple. All the problems were classic algorithm problems and belonged to the must-know category. After finishing all four problems, I still had nearly an hour left.
1133. Largest Unique Number
A warm-up problem. Traverse the array once and count. Then search from large to small for a number that satisfies the requirement.
Time complexity: O(N log N),
Space complexity: O(N).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intlargestUniqueNumber(vector<int>& A){ map<int, int> count; for (int a : A) { ++count[a]; } for (auto it = count.crbegin(); it != count.crend(); ++it) { if (it->second == 1) return it->first; } return-1; } };
1134. Armstrong Number
This still feels like an Easy problem. Just implement the statement and extract each digit of the number.
Time complexity: O(log N), the number of digits in N.
Space complexity: O(log N).
classSolution { intpow(int x, int y){ if (y == 0) return1; else return x * pow(x, y - 1); } public: boolisArmstrong(int N){ constint number = N; vector<int> digits; while (N > 0) { digits.push_back(N % 10); N /= 10; } int s = 0; int power = digits.size(); for (auto digit : digits) { s += pow(digit, power); } return s == number; } };
classSolution { structUF { vector<int> parent; int count; UF(int n) { parent.resize(n); for (int i = 0; i < n; ++i) { parent[i] = i; } count = n; } // A utility function to find the subset of an element i intfind(int i) { if (parent[i] == i) return i; returnfind(parent[i]); }
// A utility function to do union of two subsets voidUnion(int x, int y) { int xset = find(x); int yset = find(y); if(xset != yset) { parent[xset] = yset; --count; } } }; public: intminimumCost(int N, vector<vector<int>>& conections){ UF uf(N); sort(conections.begin(), conections.end(), [](constauto& lhs, constauto&rhs) -> bool { return lhs[2] < rhs[2]; }); int ans = 0; for (constauto& edge : conections) { int a = edge[0] - 1, b = edge[1] - 1, cost = edge[2]; if (uf.find(a) != uf.find(b)) { uf.Union(a, b); ans += cost; if (uf.count == 1) return ans; } } return-1; } };