-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmis.py
More file actions
60 lines (51 loc) · 1.57 KB
/
mis.py
File metadata and controls
60 lines (51 loc) · 1.57 KB
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
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# 100 pass
# 1. sum solution
n = len(nums)
return sum(range(n+1)) - sum(nums)
"""
# 2. Iteration through n - Time Limit Exceeded
n = len(nums)
for i in range(n+1):
if i not in nums:
return i
return -1
"""
"""
# 100 pass
# 3. Modify 2 to make it optimized - Using SET in python, optimizes a lot because it converts the datastructure to a hashmap and increase the access time
n = len(nums)
nums = set(nums)
for i in range(n+1):
if i not in nums:
return i
return -1
"""
"""
# 3. XOR logic
# when you exor the total length of the array again and again with (index^value), we end up in the missing number
a = len(nums)
for i in range(len(nums)):
a ^= i^nums[i]
return a
"""
"""
# 4. sort it and do it
"""
# 5. Use a dictionary
class Solution:
def missingNumber(self, nums: List[int]) -> int:
import collections
n = range(len(nums))
num = collections.Counter(n)
for i in range(len(nums)):
num[nums[i]] += 1
for k,v in num.items():
if v == 1:
return k
return n[-1]+1