-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort Colors.cpp
More file actions
33 lines (31 loc) · 886 Bytes
/
Sort Colors.cpp
File metadata and controls
33 lines (31 loc) · 886 Bytes
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
// Problem: Sort Colors (Leetcode 75)
// Array contains only 0, 1, 2 → sort in-place.
// Use Dutch National Flag Algorithm with 3 pointers:
// - low = next place for 0
// - mid = current element
// - high = next place for 2
// Rule:
// if 0 → swap with low, ++low, ++mid
// if 1 → just ++mid
// if 2 → swap with high, --high (don’t move mid)
//
// Time: O(n), Space: O(1)
class Solution {
public:
void sortColors(vector<int>& nums) {
int low = 0, mid = 0, high = nums.size() - 1;
while (mid <= high) {
if (nums[mid] == 0) {
swap(nums[low], nums[mid]);
low++; mid++;
}
else if (nums[mid] == 1) {
mid++;
}
else { // nums[mid] == 2
swap(nums[mid], nums[high]);
high--;
}
}
}
};