-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem35.js
47 lines (43 loc) · 1.3 KB
/
Problem35.js
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
// Problem 35
//
// This problem was asked by Google.
//
// 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'].
//
// https://leetcode.com/problems/sort-colors/description/
// https://en.wikipedia.org/wiki/Dutch_national_flag_problem
//
// O(N) Time Complexity
// O(1) Space Complexity
// N is the length of array
/**
* Segregates the values of the array so that all the Rs come first, Gs second, and Bs last
* @param {string[]} colors
*/
function sortColors(colors) {
let low = 0; // index of where 'R' should be replaced
let high = colors.length - 1; // index of where 'B' should be replaced
let curr = 0;
while (curr <= high) {
if (colors[curr] === 'R') {
swap(colors, curr, low);
low++;
curr++;
} else if (colors[curr] === 'B') {
swap(colors, curr, high);
high--;
} else {
curr++;
}
}
}
function swap(colors, idx1, idx2) {
const temp = colors[idx1];
colors[idx1] = colors[idx2];
colors[idx2] = temp;
}
export default sortColors;