-
-
Notifications
You must be signed in to change notification settings - Fork 155
Sort colors - Added solution to leetcode 75 #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
--- | ||
id: sort-colors | ||
title: Sort Colors | ||
difficulty: Medium | ||
sidebar_label: 0075-sortcolors | ||
tags: | ||
- Arrays | ||
- Two Pointers | ||
- Sorting | ||
--- | ||
|
||
## Problem | ||
|
||
| Problem Statement | Solution Link | LeetCode Profile | | ||
| :---------------- | :------------ | :--------------- | | ||
| [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/) | | ||
|
||
## Problem Description | ||
|
||
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. | ||
|
||
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. | ||
|
||
You must solve this problem without using the library's sort function. | ||
|
||
|
||
### Examples | ||
|
||
### Example 1: | ||
|
||
**Input**: nums = [2,0,2,1,1,0] | ||
|
||
**Output**: [0,0,1,1,2,2] | ||
|
||
|
||
### Example 2: | ||
|
||
**Input**: nums = [2,0,1] | ||
|
||
**Output**: [0,1,2] | ||
|
||
|
||
|
||
### Constraints | ||
|
||
- $n = \text{nums.length}$ | ||
- $1 \leq n \leq 300$ | ||
- $\text{nums}[i] \in \{0, 1, 2\}$ | ||
|
||
### Approach | ||
This problem is a variation of the popular Dutch National flag algorithm. | ||
|
||
The steps will be the following: | ||
|
||
- First, we will run a loop that will continue until mid <= high. | ||
- There can be three different values of mid pointer i.e. arr[mid] | ||
- 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. | ||
- 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. | ||
- 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. | ||
- Finally, our array should be sorted. | ||
### Solution Code | ||
|
||
#### Python | ||
|
||
``` | ||
class Solution: | ||
def sortArray(arr): | ||
low = 0 | ||
mid = 0 | ||
high = len(arr) - 1 | ||
|
||
while mid <= high: | ||
if arr[mid] == 0: | ||
arr[low], arr[mid] = arr[mid], arr[low] | ||
low += 1 | ||
mid += 1 | ||
elif arr[mid] == 1: | ||
mid += 1 | ||
else: | ||
arr[mid], arr[high] = arr[high], arr[mid] | ||
high -= 1 | ||
``` | ||
|
||
#### Java | ||
|
||
``` | ||
class Solution { | ||
public static void sortArray(ArrayList<Integer> arr, int n) { | ||
int low = 0, mid = 0, high = n - 1; // 3 pointers | ||
|
||
while (mid <= high) { | ||
if (arr.get(mid) == 0) { | ||
// swapping arr[low] and arr[mid] | ||
int temp = arr.get(low); | ||
arr.set(low, arr.get(mid)); | ||
arr.set(mid, temp); | ||
|
||
low++; | ||
mid++; | ||
|
||
} else if (arr.get(mid) == 1) { | ||
mid++; | ||
|
||
} else { | ||
// swapping arr[mid] and arr[high] | ||
int temp = arr.get(mid); | ||
arr.set(mid, arr.get(high)); | ||
arr.set(high, temp); | ||
|
||
high--; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
#### C++ | ||
|
||
``` | ||
class Solution { | ||
public: | ||
void sortArray(vector<int>& arr, int n) { | ||
|
||
int low = 0, mid = 0, high = n - 1; // 3 pointers | ||
|
||
while (mid <= high) { | ||
if (arr[mid] == 0) { | ||
swap(arr[low], arr[mid]); | ||
low++; | ||
mid++; | ||
} | ||
else if (arr[mid] == 1) { | ||
mid++; | ||
} | ||
else { | ||
swap(arr[mid], arr[high]); | ||
high--; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
``` | ||
|
||
### Conclusion | ||
|
||
- Time Complexity: $O(N)$, where N = size of the given array. | ||
|
||
Reason: We are using a single loop that can run at most N times. | ||
|
||
- Space Complexity: $O(1)$ as we are not using any extra space. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.