We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c64205b commit df59ae4Copy full SHA for df59ae4
C++/kth-missing-positive-number.cpp
@@ -1,24 +1,23 @@
1
// Time: O(logn)
2
// Space: O(1)
3
4
-
5
class Solution {
6
public:
7
int findKthPositive(vector<int>& arr, int k) {
8
int left = 0, right = arr.size() - 1;
9
while (left <= right) {
10
const auto& mid = left + (right - left) / 2;
11
- if (check(arr, k, mid)) {
+ if (!check(arr, k, mid)) {
12
right = mid - 1;
13
} else {
14
left = mid + 1;
15
}
16
17
- return left + k; // left ? arr[left - 1] + (k - (arr[left - 1] - ((left - 1) + 1))) : k;
+ return right + 1 + k; // right >= 0 ? arr[right] + (k - (arr[right] - (right + 1))) : k;
18
19
20
private:
21
bool check(const vector<int>& arr, int k, int x) {
22
- return arr[x] - (x + 1) >= k;
+ return arr[x] - (x + 1) < k;
23
24
};
0 commit comments