Calculate the difference between two dates. I originally wanted to compute it manually, but it was too complicated to write. Later I decisively gave up and opportunistically used Python’s date handling library functions.
there are no orphans, meaning nodes with neither a parent nor a child, when the number of nodes is greater than 1
The third requirement is relatively hard to think of; even the OJ was initially wrong. I just went along with the wrong behavior and passed. Thanks to votrubac for the hint.
classSolution { pair<int, int> current(int n){ for (int i = floor(sqrt(n)); i >= 1; --i) { if (n % i == 0) { return {i, n / i}; } } return {0, 0}; } public: vector<int> closestDivisors(int num){ auto p1 = current(num + 1); auto p2 = current(num + 2); if (abs(p1.first - p1.second) < abs(p2.second - p2.first)) { return {p1.first, p1.second}; } else { return {p2.first, p2.second}; } } };
1363. Largest Multiple of Three
Overall this is a greedy idea. First consider a simpler problem: How to form the largest array -> put the largest values first. After adding the constraint of divisibility by 3, we observe that keeping more remaining digits is better, and secondarily, removing smaller digits is better. This gives the following algorithm.
Because I mistakenly used multiset as set, I got one Wrong Answer. This is not the first time I have made a similar mistake. In the future, when considering set, I need to think in advance about whether duplicates are allowed.
Time complexity: O(n log n), space complexity: O(n).