-
Notifications
You must be signed in to change notification settings - Fork 13
/
1060-missing-element-in-sorted-array.py
72 lines (61 loc) · 1.77 KB
/
1060-missing-element-in-sorted-array.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Given a sorted array A of unique numbers,
find the K-th missing number starting from the leftmost number of the array.
Example 1:
Input: A = [4,7,9,10], K = 1
Output: 5
Explanation:
The first missing number is 5.
Example 2:
Input: A = [4,7,9,10], K = 3
Output: 8
Explanation:
The missing numbers are [5,6,8,...], hence the third missing number is 8.
Example 3:
Input: A = [1,2,4], K = 3
Output: 6
Explanation:
The missing numbers are [3,5,6,7,...], hence the third missing number is 6.
Note:
1 <= A.length <= 50000
1 <= A[i] <= 1e7
1 <= K <= 1e8
"""
class Solution:
# Time O(logn) and Space O(1)
def missingElement(self, nums, k):
mis = self.missing(nums, len(nums)-1)
if mis < k:
return nums[-1] + (k- mis)
start = 0
end = len(nums) - 1
while start < end:
mid = (start+end)//2
mis = self.missing(nums, mid)
if mis < k:
# We can't choose <= K because
# it says, it's a potential answer let's keep it and
# see if we can find bigger answer
# we will maximize the result which is not what we want
start = mid + 1
else:
end = mid
return nums[start-1] + (k-self.missing(nums, start - 1))
def missing(self, nums, index):
# K = actual present - should be present
return nums[index]- (nums[0] + index)
class solution:
# Time O(n) and Space O(1)
def missingElement(self, nums, k):
index = 0
for i in range(nums[0], nums[-1]+1):
if nums[index] == i:
index += 1
else:
k -= 1
if k == 0:
return i
while k:
i += 1
k -= 1
return i