Over the weekend, I completely wasted three contests.
I did not participate in the biweekly contest, was half an hour late to the weekly contest, and then immediately participated in Kick Start. I was already done for.
In the future, before contests, I still need to conserve energy and compete properly.
For the fourth problem, I actually had an idea at the end, but unfortunately there was not enough time. I had previously done similar problems using Trie to handle XOR, so the impression was still quite deep.
1800. Maximum Ascending Subarray Sum
A warm-up problem. Use two variables to record the current accumulated sum satisfying the increasing condition and the previous element, then update the maximum accumulated sum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intmaxAscendingSum(vector<int>& nums){ int ans = 1; int last = 0; int current = 0; for (int i : nums) { if (i > last) { current += i; } else { current = i; } ans = max(ans, current); last = i; } return ans; } };
Time complexity: O(N), Space complexity: O(1).
1801. Number of Orders in the Backlog
Use TreeMap to maintain price and quantity as key-value pairs. Update according to the problem statement.
classSolution { structTrie { array<Trie*, 2> children; int count = 0; }; constint MAX_BIT = 16; public: intcountPairs(vector<int>& nums, int low, int high){ constint n = nums.size(); Trie *root = newTrie(); for (int i : nums) { auto current = root; for (int k = MAX_BIT; k >= 0; --k) { if (current->children[(i >> k) & 1] == nullptr) { current->children[(i >> k) & 1] = newTrie(); } current = current->children[(i >> k) & 1]; current->count += 1; } } auto countLessThan = [&](constint target) -> int { int ans = 0; for (int i : nums) { auto current = root; for (int k = MAX_BIT; k >= 0 && current; --k) { constint x = (i >> k) & 1; constint y = (target >> k) & 1; if (y == 1) { if (current->children[x]) { ans += current->children[x]->count; // take all xor = zero } current = current->children[1 - x]; } else { current = current->children[x]; } } } return ans; }; return (countLessThan(high + 1) - countLessThan(low)) / 2; } };
Time complexity: O(16 N), Space complexity: O(16 N).