Sort arr2 first. Then for every element in arr1, use binary search to determine whether arr2 contains a value within distance d.
Time complexity: O(arr2.size() * log arr2.size() + arr1.size() * log arr2.size()), space complexity: O(1).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intfindTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d){ int ans = 0; sort(arr2.begin(), arr2.end()); for (int i : arr1) { auto it1 = lower_bound(arr2.begin(), arr2.end(), i - d); auto it2 = upper_bound(arr2.begin(), arr2.end(), i + d); if (it1 == it2) ++ans; } return ans; } };
1386. Cinema Seat Allocation
Greedy. For each row, try to place two families first, then try to place one family. Since each row has 10 seats, a bitmap can be used to represent occupancy. For rows without reserved seats, directly place two families.
Time complexity: O(reservedSeats.size() * log reserveredSeats.size() + max(n, reservedSeats.size())), space complexity: O(1).
classSolution { public: intmaxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats){ sort(reservedSeats.begin(), reservedSeats.end(), [](constauto& a, constauto& b) -> bool { if (a[0] == b[0]) { return a[1] < b[1]; } else { return a[0] < b[0]; } }); int ans = 0; for (int index = 0, row = 1; row <= n;) { unsignedint current_row = 0; for (; index < reservedSeats.size() && reservedSeats[index][0] == row; ++ index) { current_row |= 1 << (reservedSeats[index][1] - 1); } current_row = ~current_row; if ((current_row & 0b0111111110) == 0b0111111110) ans += 2; elseif ((current_row & 0b0001111000) == 0b0001111000) ++ans; elseif((current_row & 0b0000011110) == 0b0000011110) ++ans; elseif ((current_row & 0b0111100000) == 0b0111100000) ++ans; if (index < reservedSeats.size()) { ans += (reservedSeats[index][0] - row - 1) * 2; row = reservedSeats[index][0]; } else { ans += (n + 1 - row - 1) * 2; row = n + 1; } } return ans; } };
1387. Sort Integers by The Power Value
At first I thought of BFS, starting from 1. But it timed out, and the index of the final return value was also not easy to handle, wasting a lot of debugging time.
Time complexity: O(2 ^ value of Kth), space complexity: O(2 ^ value of Kth).
classSolution { public: intgetKth(int lo, int hi, int k){ unordered_map<int, int> memo; function<int(int)> recurse = [&](int index) -> int { if (index == 1) return0; elseif (memo.find(index) != memo.end()) { return memo[index]; } else { if (index % 2 == 0) return memo[index] = recurse(index / 2) + 1; else return memo[index] = recurse(3 * index + 1) + 1; } }; vector<int> v; for (int i = lo; i <= hi; ++i) { v.push_back(i); } sort(v.begin(), v.end(), [&](int a, int b) -> bool { int a_value = recurse(a); int b_value = recurse(b); if (a_value != b_value) return a_value < b_value; else return a < b; }); return v[k - 1]; } };
1388. Pizza With 3n Slices
Because the previous problems took too much time, only 10 minutes were left for problem 4. I glanced at the statement, had no idea, and gave up. In fact, if the problem is transformed a bit, the solution becomes clear.
The problem can be transformed into: choose n elements from an array of length 3*n, requiring that chosen elements are not adjacent and that the head and tail cannot both be chosen, maximizing the resulting sum. It is similar to 213. House Robber II. State transition equation:
1 2 3 4 5
f(begin, end, remain, cycle) = max{ nums[end] + f(begin + cycle, end - 2, remain - 1, 0), // take end f(begin, end - 1, remain, 0) // not take end }
Time complexity: O(N ^ 3), space complexity: O(N ^ 3).