-
Notifications
You must be signed in to change notification settings - Fork 0
/
top_interview_150__80_remove-duplicates-from-sorted-array-ii.py
79 lines (66 loc) · 1.56 KB
/
top_interview_150__80_remove-duplicates-from-sorted-array-ii.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
73
74
75
76
77
78
79
from typing import List
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
print(nums)
i = 0
k = 0
counter1 = {}
n = len(nums)
for item in nums:
if item in counter1:
counter1[item] = counter1[item] + 1
else:
counter1[item] = 1
#print(f"{left=}, {i=},{cnt[left]=}, {k=}, {right=} ")
#print(f"{left=}, {i=},{cnt[left]=}, {k=}, {right=} \n" + "#"*30)
items = list(counter1.keys())
m = len(items)
print(items)
print(counter1)
while k < m:
if counter1[items[k]] == 1:
nums[i] = items[k]
i+=1
k+=1
else:
nums[i] = items[k]
i+=1
nums[i] = items[k]
i+=1
k+=1
print(nums)
sol = Solution()
nums = [1,1,1,2,2,3]
sol.removeDuplicates(nums)
nums = [1,1,1,1,2,2,3]
sol.removeDuplicates(nums)
nums = [1,2,2,2,2,3,4,4,4,5,5,5,6,8]
sol.removeDuplicates(nums)
"""
[1,1,1,2,2,3]
^ ^
cnt=1
cmp: left == right => inc
[1,1,1,2,2,3]
^ ^
cnt=2
cmp: left == right
We need to increment k until a bigger integer appears
[1,1,1,2,2,3]
^ ^
cnt = 2
cmp: left < right
increment i, swap, increment k
[1,1,1,2,2,3]
^ ^
[1,1,2,2,2,3]
^ ^
[1,1,2,2,2,3]
^ ^
[1,1,1,2,2,3]
k i
[1,1,1,2,2,3]
k i
[1,1,1,2,2,3]
k i
"""