This is one of those problems where the clean idea matters more than the amount of code. For 41. First Missing Positive, the solution in this repository is mainly a greedy 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: greedy.
The notes already sitting in the source point us in the right direction:
- sorting
- time: O(NlogN), space: O(N)
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 firstMissingPositive, vnums.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- 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), because in each iteration of while loop, we put one element into its right position,
- Space: O(1)
Guide
C++ Solution
Your submission
The accepted solution
01//sorting
02//Runtime: 4 ms, faster than 83.82% of C++ online submissions for First Missing Positive.
03//Memory Usage: 10.1 MB, less than 8.45% of C++ online submissions for First Missing Positive.
04//time: O(NlogN), space: O(N)
05class Solution {
06public:
07 int firstMissingPositive(vector<int>& nums) {
08 set<int> unums;
09
10 for(int& num : nums){
11 if(num > 0){
12 unums.insert(num);
13 }
14 }
15
16 if(unums.size() == 0){
17 return 1;
18 }
19
20 vector<int> vnums(unums.begin(), unums.end());
21
22 sort(vnums.begin(), vnums.end());
23
24 if(vnums[0] > 1){
25 return 1;
26 }
27
28 for(int i = 0; i < vnums.size(); ++i){
29 if(vnums[i] != i+1){
30 return i+1;
31 }
32 }
33
34 return vnums.size()+1;
35 }
36};
37
38//swap element to its right place
39//https://leetcode.com/problems/first-missing-positive/discuss/17071/My-short-c%2B%2B-solution-O(1)-space-and-O(n)-time
40//Runtime: 4 ms, faster than 83.82% of C++ online submissions for First Missing Positive.
41//Memory Usage: 9.8 MB, less than 63.74% of C++ online submissions for First Missing Positive.
42//time: O(N), because in each iteration of while loop, we put one element into its right position,
43//and after an element is in its right position, we won't do anything with it,
44//and there are N elements, so the time complexity is O(N)
45//space: O(1)
46class Solution {
47public:
48 int firstMissingPositive(vector<int>& nums) {
49 int n = nums.size();
50
51 /*
52 if nums is [1,n],
53 then the first missing positive number is n+1,
54 otherwise there's some number in [`,n] missing in nums,
55 so the first missing positive number is in [1,n].
56 To summary, first missing positive number's range
57 should be in 1 to n+1.
58 */
59 for(int i = 0; i < n; ++i){
60 /*
61 swap every number in [1,n] to its correct position
62 for example, if we find a 5,
63 we should put it into position 4
64
65 nums[i] should be put in the 'nums[i]-1'th position
66 */
67 /*
68 it's "while", not "if"!!!
69 that is because we also want to put
70 the swapped element in the right place
71 for the testcase [-1,4,3,1],
72 it will become [-1,1,3,4] -> [1,-1,3,4]
73 */
74 while(nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i]-1]){
75 swap(nums[i], nums[nums[i]-1]);
76 // for(int num : nums){
77 // cout << num << " ";
78 // }
79 // cout << endl;
80 }
81 }
82
83 for(int i = 0; i < n; ++i){
84 /*
85 the number i+1 is not in its correct position,
86 so it must be missing
87 */
88 if(nums[i] != i+1){
89 return i+1;
90 }
91 }
92
93 return n+1;
94 }
95};
Cost