-
Notifications
You must be signed in to change notification settings - Fork 95
/
035.py
43 lines (32 loc) · 973 Bytes
/
035.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
"""
Problem:
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of
the array so that all the Rs come first, the Gs come second, and the Bs come last. You
can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should become
['R', 'R', 'R', 'G', 'G', 'B', 'B'].
"""
from typing import List
def segregate(arr: List[str]) -> None:
length = len(arr)
pos = 0
# pass for segregating "R"s
for i in range(length):
if arr[i] == "R":
arr[i], arr[pos] = arr[pos], arr[i]
pos += 1
# pass for segregating "G"s
for i in range(pos, length):
if arr[i] == "G":
arr[i], arr[pos] = arr[pos], arr[i]
pos += 1
if __name__ == "__main__":
arr = ["G", "B", "R", "R", "B", "R", "G"]
segregate(arr)
print(arr)
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""