This is one of those problems where the clean idea matters more than the amount of code. For 1539. Kth Missing Positive Number, the solution in this repository is mainly a two pointers solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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: two pointers.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findKthPositive.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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(logN), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 8 ms, faster than 100.00% of C++ online submissions for Kth Missing Positive Number.
02//Memory Usage: 9.9 MB, less than 50.00% of C++ online submissions for Kth Missing Positive Number.
03class Solution {
04public:
05 int findKthPositive(vector<int>& arr, int k) {
06 int count = 0;
07
08 for(int i = 0; i < arr.size(); ++i){
09 if(i == 0){
10 //from 1 to arr[0]-1
11 if(count + arr[0]-1 >= k){
12 return k;
13 }
14 count += arr[0]-1;
15 }else{
16 //from arr[i-1]+1 to arr[i]
17 if(count + arr[i] - arr[i-1] - 1 >= k){
18 return arr[i-1] + (k - count);
19 }
20 count += arr[i] - arr[i-1] - 1;
21 }
22 // cout << i << " -> " << count << endl;
23
24 // if(count >= k){
25 // return arr[i] + (count-k);
26 // }
27 }
28
29 //count < k
30 return arr.back() + (k-count);
31 }
32};
33
34//not understand
35//Binary search
36//https://leetcode.com/problems/kth-missing-positive-number/discuss/779999/JavaC%2B%2BPython-O(logN)
37//Runtime: 8 ms, faster than 100.00% of C++ online submissions for Kth Missing Positive Number.
38//Memory Usage: 9.7 MB, less than 50.00% of C++ online submissions for Kth Missing Positive Number.
39//time: O(logN), space: O(1)
40class Solution {
41public:
42 int findKthPositive(vector<int>& arr, int k) {
43 //i: consider arr[1...i], index is 1-based
44 int l = 0, r = arr.size();
45 int m;
46
47 while(l < r){
48 m = (l+r+1) >> 1;
49 // cout << l << ", " << m << ", " << r << endl;
50 // if(m > 0) cout << "m: " << arr[m-1] - m << endl;
51
52 /*
53 arr[1...m] - m:
54 in the range arr[1...m](index is 1-based),
55 there are m numbers not missing,
56 so the number of missing numbers is arr[m] - m
57 */
58 if(m == 0 || arr[m-1] - m < k){
59 l = m;
60 }else{
61 r = m-1;
62 }
63 }
64
65 return l + k;
66 }
67};
Cost