-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremove-duplicates-from-sorted-array.py
102 lines (93 loc) · 3.44 KB
/
remove-duplicates-from-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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 26. Remove Duplicates from Sorted Array
# 🟢 Easy
#
# https://leetcode.com/problems/remove-duplicates-from-sorted-array/
#
# Tags: Array - Two Pointers
import timeit
from typing import List
# Use a read and write pointers, read each value and, when the values
# are unique, write them to the corresponding position.
#
# Time complexity: O(n) - We visit each element once.
# Space complexity: O(1) - Constant space, if we don't consider input
# and output arrays.
class NonMutating:
def removeDuplicates(self, nums: List[int]) -> int:
# Use two pointers, one to read and one to write.
w = r = 0
# Initialize a variable that registers the last number processed.
last = float("-inf")
while r < len(nums):
# If this is the first occurrence of a number, we want to
# save it to the result array.
if nums[r] != last:
# Mark this value as the last seen.
last = nums[r]
# Write the value to its position and update the
# write pointer.
nums[w] = nums[r]
w += 1
# Always update the write pointer.
r += 1
# Overwrite the rest of the values with None.
for idx in range(w, len(nums)):
nums[idx] = None
return nums
# This solution is modified to work in the LeetCode tests, they ask for
# the array to be modified in place, and the last values can be
# anything, we do not need to set them to None.
#
# Time complexity: O(n) - We visit each element once.
# Space complexity: O(1) - Constant space.
#
# Runtime: 154 ms, faster than 50.33%
# Memory Usage: 15.5 MB, less than 65.27%
#
# NOTE: This solution does not work locally, it is modified to work with
# the LeetCode tests, it modifies the input in place and returns the
# number of unique elements.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
# Use two pointers, one is the implicit read pointer of the
# for loop, other is the explicit write pointer initialized to 0.
w = 0
# Initialize a variable that registers the last number processed.
last = float("-inf")
for val in nums:
# If this is the first occurrence of a number, we want to
# save it to the result array.
if val != last:
# Mark this value as the last seen.
last = val
# Write the value to its position and update the
# write pointer.
nums[w] = val
w += 1
return w
def test():
executors = [NonMutating]
tests = [
[[1, 1, 2], [1, 2, None]],
[
[0, 0, 1, 1, 1, 2, 2, 3, 3, 4],
[0, 1, 2, 3, 4, None, None, None, None, None],
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.removeDuplicates(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()