A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 659. Split Array into Consecutive Subsequences, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- binary search
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are isPossible.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//binary search
02//Runtime: 320 ms, faster than 15.61% of C++ online submissions for Split Array into Consecutive Subsequences.
03//Memory Usage: 54.4 MB, less than 20.00% of C++ online submissions for Split Array into Consecutive Subsequences.
04class Solution {
05public:
06 bool isPossible(vector<int>& nums) {
07 vector<int> tails; //should be sorted in ascending order
08 vector<int>::iterator it;
09
10 while(!nums.empty()){
11 int head = nums[0];
12 //find the "last"(acheived by prev(upper_bound(...))) head-1 in tails,
13 if(tails.size() == 0 || (it = upper_bound(tails.begin(), tails.end(), head-1)) == tails.begin() || (*(it = prev(it)) != head-1)){
14 // cout << "cannot find " << head-1 << " in tails? " << (it == tails.begin()) << endl;
15 //if head-1 not in tails
16 nums.erase(nums.begin());
17 it = find(nums.begin(), nums.end(), head+1);
18 if(it == nums.end()) return false;
19 nums.erase(it);
20 it = find(nums.begin(), nums.end(), head+2);
21 if(it == nums.end()) return false;
22 nums.erase(it);
23 //need a new subsequence
24 tails.push_back(head+2);
25 // cout << "nums.size(): " << nums.size() << endl;
26 }else{
27 // cout << "cannot find " << head-1 << " in tails? " << (it == tails.begin()) << endl;
28 //append to a old subsequence
29 /*
30 note that since "it" points to the last "head-1" in tails,
31 so tails can be kept "ascending"
32 */
33 *it = head;
34 it = find(nums.begin(), nums.end(), head);
35 if(it == nums.end()) return false;
36 nums.erase(it);
37 }
38
39// for(int num : nums){
40// cout << num << " ";
41// }
42// cout << endl;
43
44// for(int tail : tails){
45// cout << tail << " ";
46// }
47// cout << endl;
48 }
49
50 return true;
51 }
52};
Cost