-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
33 lines (28 loc) · 825 Bytes
/
solver.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
class Solution():
def sortNumsV1(self, arr):
"""
Time complexity: O(n)
Space complexity: O(n) (result is a new sorted array)
"""
occurrences = [0, 0, 0] # ["occurrences #1", "occurences #2", "occurrences #3"]
for num in arr:
dic[num-1] += 1
return [1] * dic[0] + [2] * dic[1] + [3] * dic[2]
def sortNumsV2(self, arr):
"""
Time complexity: O(n)
Space complexity: O(1) (sort is performed in place)
"""
left, right = 0, len(arr) - 1
index = 0
while index <= right:
if arr[index] == 1:
arr[left], arr[index] = arr[index], arr[left]
left += 1
index += 1
elif arr[index] == 2:
index += 1
elif arr[index] == 3:
arr[right], arr[index] = arr[index], arr[right]
right -= 1
return arr