The main mistake in this contest was that I wrote the cmp function in sort incorrectly for the second problem and did not guarantee strict ordering. It kept causing segmentation faults. That is, if a < b, then necessarily b !< a.
1093. Statistics from a Large Sample
Intution: You only need to be familiar with C++ and the meaning of these five statistical values. Time complexity: O(N), Space complexity: O(1).
classSolution { public: vector<double> sampleStats(vector<int>& count){ double minimum = 255.0, maximum = 0.0, mean = 0.0, median = 0.0, mode = 0.0; auto minimum_it = find_if(count.cbegin(), count.cend(), [](constauto & a) -> bool { return a > 0; }); minimum = minimum_it - count.cbegin(); auto max_it = find_if(count.crbegin(), count.crend(), [](constauto & a) -> bool { return a > 0; }); maximum = static_cast<int>(count.size()) - (max_it - count.crbegin()) - 1; int num = 0; for (int i = 0; i < count.size(); ++i) { mean += i * count[i]; num += count[i]; } mean /= num; int total = num; num = 0; for (int i = 0; i < count.size(); ++i) { if (count[i] == 0) continue; num += count[i]; if (num * 2 > total) { if (median > 0) { median = (median + i) / 2; } else { median = i; } break; } elseif (num * 2 == total) { median = i; } } auto mode_it = max_element(count.cbegin(), count.cend()); mode = mode_it - count.cbegin(); return {minimum, maximum, mean, median, mode}; } };
1094. Car Pooling
Intuition: Simulate the whole process of passengers getting on and off. At each stop, check whether the capacity is exceeded. Time complexity: O(N log N) Space complexity: O(N)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: boolcarPooling(vector<vector<int>> &trips, int capacity){ map<int, int> stops; for (constauto& trip : trips) { stops[trip[1]] += trip[0]; stops[trip[2]] -= trip[0]; } int passengers = 0; for (constauto& stop : stops) { passengers += stop.second; if (passengers > capacity) returnfalse; } returntrue; } };
1095. Find in Mountain Array
This problem counts as a new type of problem: an interactive problem. Three binary searches solve it.
Time complexity: O(log N). Space complexity: O(1).
/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * class MountainArray { * public: * int get(int index); * int length(); * }; */ classSolution { public: intfindInMountainArray(int target, MountainArray &mountainArr){ int lo = 0, hi = mountainArr.length(); while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mid == mountainArr.length() - 1) { hi = mid; continue; } if (mountainArr.get(mid) < mountainArr.get(mid + 1)) lo = mid + 1; else hi = mid; } int mountain = lo; // cout << "mountain: " << mountain << endl; lo = 0, hi = mountain + 1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mountainArr.get(mid) < target) lo = mid + 1; else hi = mid; } // cout << "first: " << lo << endl; if (mountainArr.get(lo) == target) return lo; lo = mountain, hi = mountainArr.length(); while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mountainArr.get(mid) > target) lo = mid + 1; else hi = mid; } // cout << "second: " << lo << endl; if (lo < mountainArr.length() && mountainArr.get(lo) == target) return lo; return-1; } };
1096. Brace Expansion II
Referenced this discuss. The author used parsing to solve this problem. The two subroutines parseRule2 and parseRule3 handle union and Cartesian product respectively.
Time complexity: O(N^N), because of the Cartesian product.