LeetCode opened its first biweekly contest, every Saturday night from 10:30 to 12:30. The goal may be to make it easier for students in Europe to participate. The normal weekly contest is usually in the early morning in Europe. With the duration extended to two hours, they can also set harder problems.
Since I had already taken ByteDance’s summer camp written test from 19:00 to 21:30, and that written test was also very hard, with only 30% of the second of three programming questions passed, the later biweekly contest also went badly. My result was not ideal. I only solved two Easy problems.
1055. Fixed Point
Intuition: Straight forward. One pass.
Time complexity: O(N) Space complexity: O(1)
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: intfixedPoint(vector<int>& A){ for (int i = 0; i < A.size(); ++i) { if (A[i] == i) { return i; } } return-1; } };
Because array A is sorted in ascending order, we can also use binary search to implement O(log N).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intfixedPoint(vector<int>& A){ int lo = 0, hi = A.size(); while (lo < hi) { int mid = lo + (hi - lo) / 2; if (A[mid] < mid) { lo = mid + 1; } else { hi = mid; } } return A[lo] == lo ? lo : -1; } };
1056. Index Pairs of a String
Intuition: The problem is Easy and the data size is also small, so a brute-force solution is enough.
Time complexity: O(words.size() * text.size() * words[i].size()), Space complexity: O(words.size() * text.size()).
intcountDigitOne(int n) { int countr = 0; for (longlong i = 1; i <= n; i *= 10) { longlong divider = i * 10; countr += (n / divider) * i + min(max(n % divider - i + 1, 0LL), i); } return countr; }
Based on the solution above, it is easy to extend to any digit.
One thing to note is that digit 0 cannot be a leading digit, so it needs special handling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { // include n inthelper(int d, int n){ int ret = 0; for (longlong i = 1; i <= n; i *= 10) { longlong divisor = i * 10; ret += (n / divisor) * i + min(i, max(0LL, n % divisor - d * i + 1)) - (d == 0 ? i : 0); // cout << ret << " "; } // cout << n << " " << ret << endl; return ret; } public: intdigitsCount(int d, int low, int high){ returnhelper(d, high) - helper(d, low - 1); } };