- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 39 lines of C++ from the credited upstream file 3489.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 6 loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) {4 if (all_of(nums.begin(), nums.end(), [](int num) { return num == 0; }))5 return 0;6 7 const int n = nums.size();8 vector<unordered_set<int>> subsetSums(n);9 10 for (int i = 0; i < n; ++i)11 subsetSums[i].insert(0);12 13 for (int k = 0; k < queries.size(); ++k) {14 const int l = queries[k][0];15 const int r = queries[k][1];16 const int val = queries[k][2];17 for (int i = l; i <= r; ++i) {18 vector<int> sums;19 for (const int subsetSum : subsetSums[i])20 sums.push_back(subsetSum + val);21 for (const int sum : sums)22 subsetSums[i].insert(sum);23 }24 if (canFormAllNumbers(subsetSums, nums))25 return k + 1;26 }27 28 return -1;29 }30 31 bool canFormAllNumbers(const vector<unordered_set<int>>& subsetSums,32 const vector<int>& nums) {33 for (int i = 0; i < nums.size(); ++i)34 if (!subsetSums[i].contains(nums[i]))35 return false;36 return true;37 }38};39