-
Notifications
You must be signed in to change notification settings - Fork 3
/
0143. Sort Colors II.java
67 lines (64 loc) · 2.05 KB
/
0143. Sort Colors II.java
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
56
57
58
59
60
61
62
63
64
65
66
67
public class Solution {
/**
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] colors, int k) {
int pos = 0;
for(int c = 1; c <= k; c++){
for(int i = 0; i < colors.length; i++){
if(colors[i] == c){
swap(colors, i, pos++);
}
}
}
}
public void swap(int[] colors, int a, int b){
int temp = colors[a];
colors[a] = colors[b];
colors[b] = temp;
}
}
//less cringey solution :D
public class Solution {
/**
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] colors, int k) {
if(colors.length < 2){
return;
}
for(int c = 0; c < k; c++){
if(colors[c] < 0) continue; //colors[c] is in the right place
int lost = colors[c];
int nextLoc = lost-1; //where lost SHOULD be
colors[c] = 0; //marked as empty home
while(lost > 0){
if(colors[nextLoc] > 0){ //another is is lost's place
System.out.println("hi " + lost + " " + nextLoc);
lost = colors[nextLoc]; //new body is now marked as lost
colors[nextLoc] = -1; //initial lost takes residence
nextLoc = lost-1;
// System.out.println(Arrays.toString(colors) + " " + nextLoc);
} else {
colors[nextLoc]--; //one more resident of this home
break;
}
}
}
for(int i = k; i < colors.length; i++){
colors[colors[i]-1]--; //marks frqs of all others in array
}
int pos = colors.length-1;
for(int i = k-1; i>=0;i--){
int col = i + 1;
int frq = -colors[i];
for(int j = 0; j < frq; j++){
colors[pos--] = col;
}
}
}
}