-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0075.java
More file actions
68 lines (64 loc) · 2.17 KB
/
_0075.java
File metadata and controls
68 lines (64 loc) · 2.17 KB
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
68
package com.github.aditya;
public class _0075 {
// 1. One Pass - 0ms, faster than 100%
// i and j - two pointers at start and end of the array, k to loop
// swap i and current num (k) when 0 found and swap j and k when 2 is found
public static class Solution {
public void sortColors(int[] nums) {
int i = 0, j = nums.length - 1, k = 0;
while (k < nums.length) {
if (nums[k] == 0 && k != i) {
swap(nums, i, k);
i++;
} else if (nums[k] == 2 && k < j) {
swap(nums, j, k);
j--;
} else
k++;
}
}
public void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
// 2. Two Pass - 0 ms, faster than 100% and memory 37.2 MB, less than 96.54%
// In the first pass - collect all the 0s - two adjacent pointers i and j
// If i is already 0 [Ex - 0,0,2...] i++ and j++ and continue
// When j encounters 0 just swap contents of i and j
// In the second pass repeat the same logic for 1.
public static class Solution_1 {
public void sortColors(int[] nums) {
int i = 0, j = 1;
for (int k = 0; i < nums.length && j < nums.length && k < nums.length; k++) {
if (nums[i] == 0) {
i++; j++;
continue;
}
if (nums[j] == 0) {
swap(nums, i, j);
i++;
}
j++;
}
j = i + 1;
for (int k = 0; i < nums.length && j < nums.length && k < nums.length; k++) {
if (nums[i] == 1) {
i++; j++;
continue;
}
if (nums[j] == 1) {
swap(nums, i, j);
i++;
}
j++;
}
}
public void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
}