← Home

80. Remove Duplicates from Sorted Array II

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
80

This is one of those problems where the clean idea matters more than the amount of code. For 80. Remove Duplicates from Sorted Array II, 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.

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 removeDuplicates.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • 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:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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

solution.cpp
01//Runtime: 12 ms, faster than 46.22% of C++ online submissions for Remove Duplicates from Sorted Array II.
02//Memory Usage: 11.3 MB, less than 13.64% of C++ online submissions for Remove Duplicates from Sorted Array II.
03class Solution {
04public:
05    int removeDuplicates(vector<int>& nums) {
06        int n = nums.size();
07        int slow = 0, fast = 0;
08        bool first;
09        
10        while(fast < n){
11            // cout << slow << " <- " << fast << endl;
12            nums[slow] = nums[fast];
13            if(slow == 0 || nums[slow] != nums[slow-1]){
14                first = true;
15            }else{
16                first = false;
17            }
18            
19            if(first){
20                ++fast;
21            }else{
22                while(fast < n && nums[fast] == nums[slow]){
23                    ++fast;
24                }
25            }
26            
27            ++slow;
28        }
29        
30        return slow;
31    }
32};
33
34//https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby
35//Runtime: 4 ms, faster than 99.33% of C++ online submissions for Remove Duplicates from Sorted Array II.
36//Memory Usage: 11.3 MB, less than 14.37% of C++ online submissions for Remove Duplicates from Sorted Array II.
37class Solution {
38public:
39    int removeDuplicates(vector<int>& nums) {
40        int n = nums.size();
41        int slow = 0;
42        int k = 2;
43        
44        for(int fast = 0; fast < n; ++fast){
45            if(slow < k || nums[fast] != nums[slow-k]){
46                /*
47                only update nums[slow] when 
48                there are < k nums[fast] in the new array
49                */
50                nums[slow++] = nums[fast];
51            }
52        }
53        
54        return slow;
55    }
56};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.