-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
32 lines (27 loc) · 913 Bytes
/
Solution.cs
File metadata and controls
32 lines (27 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace LeetCode.Problem1539{
//1422. Kth Missing Positive Number
//https://leetcode.com/problems/kth-missing-positive-number/
/*
Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.
Return the kth positive integer that is missing from this array.
*/
public class Solution {
public int FindKthPositive(int[] arr, int k) {
int index = 1;
int countOfMissed = arr.LastOrDefault() - arr.Length;
if (countOfMissed == 0)
return k + arr.LastOrDefault();
if (k > countOfMissed )
return arr.LastOrDefault() + (k - countOfMissed);
while (k > 0)
{
var t = arr.Select(p=>p).Where(p=> p == index).Any();
if (t == false) {
k--;
}
index++;
}
return index-1;
}
}
}