Skip to content

Commit e4644a0

Browse files
committed
optimizations
1 parent 34c209d commit e4644a0

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

find_unique_id.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def generate_log():
1717

1818
def find_unique_id_cycling(log):
1919
"""
20+
Iterates through the whole list once for each of the items in the list. Adds 1 to a counter per iteration through
21+
the list and if the counter = 1, that is the unique instance.
2022
time complexity: n^2
2123
space complexity: 1
2224
"""
@@ -37,6 +39,8 @@ def find_unique_id_cycling(log):
3739
def find_unique_cycling_reduction(log):
3840
"""
3941
time complexity: (((n-1) / 2) ^ 2) + (n-1) / 2
42+
why? n - 1 / 2 is the number of ids, ^2 because you need to trace through the list recursively but /2 because
43+
you're reducing the list as you go
4044
space complexity: 1
4145
"""
4246
copied_log = log.copy()
@@ -74,18 +78,18 @@ def find_unique_dict(log):
7478
def find_unique_sort(log):
7579
"""
7680
time complexity: nlogn + (n-1)
77-
space complexity: n
81+
space complexity: 1
7882
"""
79-
sorted_log = sorted(log)
83+
log.sort()
8084
for a in range(len(log)):
8185
try:
82-
if sorted_log[0] == sorted_log[1]:
83-
sorted_log.pop(1)
84-
sorted_log.pop(0)
86+
if log[0] == log[1]:
87+
log.pop(1)
88+
log.pop(0)
8589
else:
86-
return sorted_log[a]
90+
return log[a]
8791
except IndexError:
88-
return sorted_log[0]
92+
return log[0]
8993

9094

9195
def find_unique_crazy_xor(delivery_ids):

0 commit comments

Comments
 (0)