-
Notifications
You must be signed in to change notification settings - Fork 24
/
75 - Sort Colors.py
55 lines (51 loc) · 1.47 KB
/
75 - Sort Colors.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
# Solution 1: DNF - Dutch National Flag Problem
# We can solve this in O(n) time and O(1) memory using the following algorithm
# i - keeps track of 0's
# k - keeps track of 2's
# j is the element we are comparing
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i = 0
j = 0
k = len(nums)-1
while j <= k:
if nums[j] == 0:
self.swap(nums, i, j)
i += 1
j += 1
elif nums[j] == 2:
self.swap(nums,j,k)
k -= 1
else:
j += 1
def swap(self, nums, i, j):
tmp = nums[i]
nums[i] = nums[j]
nums[j] = tmp
# Solution 2: If you want to make it slightly faster, we can remove the swap since
# we know what the values will be
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i = 0
j = 0
k = len(nums)-1
while j <= k:
if nums[j] == 0:
nums[j] = nums[i]
nums[i] = 0
i += 1
j += 1
elif nums[j] == 2:
nums[j] = nums[k]
nums[k] = 2
k -= 1
else:
j += 1