Skip to content

Commit a8e6557

Browse files
author
Joseph Luce
authored
Update 075_sort_colors.md
1 parent 0257933 commit a8e6557

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

leetcode/medium/075_sort_colors.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# 75. Sort Colors
22

3-
## Two pointer one pass solution
3+
## Three pointer one pass solution
44

55
- Runtime: O(N)
66
- Space: O(1)
77
- N = Number of elements in list
88

99
Since we know the range of numbers, 0, 1 and 2, we can just use two pointers for each end of the array.
1010
The left pointer will point to the first unsorted number != 0 and the right pointer will point to the last unsorted number != 2.
11-
We will then traverse the array from left to right swapping the values with the current pointer to either the left or right depending if its a 0 or 2, ignoring 1s, then incrementing the left or right pointer.
11+
We will then traverse the array from left to right swapping the values with a third pointer to either the left or right depending if its a 0 or 2, ignoring 1s, then incrementing the left or right pointer.
1212
Eventually, when we reach the right pointer, it would have sorted the entire array and all the 1s will be in the middle.
1313

1414
```
@@ -17,15 +17,8 @@ class Solution:
1717
"""
1818
Do not return anything, modify nums in-place instead.
1919
"""
20-
left, right = 0, len(nums)-1
21-
while left > right:
22-
if nums[left] == 0:
23-
left += 1
24-
elif nums[right] == 2:
25-
right -= 1
26-
else:
27-
break
28-
curr_index = left
20+
right = len(nums)-1
21+
curr_index = left = 0
2922
while curr_index <= right:
3023
if nums[curr_index] == 0:
3124
nums[curr_index], nums[left] = nums[left], nums[curr_index]

0 commit comments

Comments
 (0)