Skip to content

Commit fc10d14

Browse files
authored
Merge pull request #899 from debangi29/sort-colors
Sort colors - Added solution to leetcode 75
2 parents c75bd26 + 20714ed commit fc10d14

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
id: sort-colors
3+
title: Sort Colors
4+
difficulty: Medium
5+
sidebar_label: 0075-sortcolors
6+
tags:
7+
- Arrays
8+
- Two Pointers
9+
- Sorting
10+
---
11+
12+
## Problem
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :---------------- | :------------ | :--------------- |
16+
| [Sort Colors](https://leetcode.com/problems/sort-colors/description/) | [Sort Colors Solution on LeetCode](https://leetcode.com/problems/sort-colors/solutions/) | [Leetcode Profile](https://leetcode.com/u/debangi_29/) |
17+
18+
## Problem Description
19+
20+
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
21+
22+
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
23+
24+
You must solve this problem without using the library's sort function.
25+
26+
27+
### Examples
28+
29+
### Example 1:
30+
31+
**Input**: nums = [2,0,2,1,1,0]
32+
33+
**Output**: [0,0,1,1,2,2]
34+
35+
36+
### Example 2:
37+
38+
**Input**: nums = [2,0,1]
39+
40+
**Output**: [0,1,2]
41+
42+
43+
44+
### Constraints
45+
46+
- $n = \text{nums.length}$
47+
- $1 \leq n \leq 300$
48+
- $\text{nums}[i] \in \{0, 1, 2\}$
49+
50+
### Approach
51+
This problem is a variation of the popular Dutch National flag algorithm.
52+
53+
The steps will be the following:
54+
55+
- First, we will run a loop that will continue until `mid <= high.`
56+
- There can be three different values of mid pointer i.e. arr[mid]
57+
- If arr[mid] == 0, we will swap arr[low] and arr[mid] and will increment both low and mid. Now the subarray from index 0 to (low-1) only contains 0.
58+
- If arr[mid] == 1, we will just increment the mid pointer and then the index (mid-1) will point to 1 as it should according to the rules.
59+
- If arr[mid] == 2, we will swap arr[mid] and arr[high] and will decrement high. Now the subarray from index high+1 to (n-1) only contains 2.In this step, we will do nothing to the mid-pointer as even after swapping, the subarray from mid to high(after decrementing high) might be unsorted. So, we will check the value of mid again in the next iteration.
60+
- Finally, our array should be sorted.
61+
### Solution Code
62+
63+
#### Python
64+
65+
```
66+
class Solution:
67+
def sortArray(arr):
68+
low = 0
69+
mid = 0
70+
high = len(arr) - 1
71+
72+
while mid <= high:
73+
if arr[mid] == 0:
74+
arr[low], arr[mid] = arr[mid], arr[low]
75+
low += 1
76+
mid += 1
77+
elif arr[mid] == 1:
78+
mid += 1
79+
else:
80+
arr[mid], arr[high] = arr[high], arr[mid]
81+
high -= 1
82+
```
83+
84+
#### Java
85+
86+
```
87+
class Solution {
88+
public static void sortArray(ArrayList<Integer> arr, int n) {
89+
int low = 0, mid = 0, high = n - 1; // 3 pointers
90+
91+
while (mid <= high) {
92+
if (arr.get(mid) == 0) {
93+
// swapping arr[low] and arr[mid]
94+
int temp = arr.get(low);
95+
arr.set(low, arr.get(mid));
96+
arr.set(mid, temp);
97+
98+
low++;
99+
mid++;
100+
101+
} else if (arr.get(mid) == 1) {
102+
mid++;
103+
104+
} else {
105+
// swapping arr[mid] and arr[high]
106+
int temp = arr.get(mid);
107+
arr.set(mid, arr.get(high));
108+
arr.set(high, temp);
109+
110+
high--;
111+
}
112+
}
113+
}
114+
}
115+
```
116+
117+
#### C++
118+
119+
```
120+
class Solution {
121+
public:
122+
void sortArray(vector<int>& arr, int n) {
123+
124+
int low = 0, mid = 0, high = n - 1; // 3 pointers
125+
126+
while (mid <= high) {
127+
if (arr[mid] == 0) {
128+
swap(arr[low], arr[mid]);
129+
low++;
130+
mid++;
131+
}
132+
else if (arr[mid] == 1) {
133+
mid++;
134+
}
135+
else {
136+
swap(arr[mid], arr[high]);
137+
high--;
138+
}
139+
}
140+
}
141+
};
142+
143+
```
144+
145+
### Conclusion
146+
147+
- Time Complexity: $O(N)$, where N = size of the given array.
148+
149+
Reason: We are using a single loop that can run at most N times.
150+
151+
- Space Complexity: $O(1)$ as we are not using any extra space.

0 commit comments

Comments
 (0)