-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing-number.py3
More file actions
40 lines (29 loc) · 904 Bytes
/
missing-number.py3
File metadata and controls
40 lines (29 loc) · 904 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
33
34
35
36
37
38
39
# 268. Missing Number (6/10/57390)
# Runtime: 0 ms (94.92%) Memory: 18.48 MB (92.00%)
# Time Complexity O(n log n)
# Space Complexity O(1)
# A simple solution could be to sort the array
# then iterate while checking if nums[i] - i >= 1
# class Solution:
# def missingNumber(self, nums: List[int]) -> int:
# nums.sort()
# for i in range(len(nums)):
# if nums[i] - i >= 1:
# return i
# return len(nums)
# Time Complexity O(n)
# Space Complexity O(1)
# A better solution is to use math
# sum of n elements = n*(n+1)/2
# sum(n) - sum(nums) = missing number
# [3,0,1]
# sumN = 3*4/2 = 6
# sumNums= (3+0+1) = 4
# missing = 6 - 4 = 2
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
sumN = (n * (n + 1)) // 2
sumNums = sum(nums)
missing = sumN - sumNums
return missing