From 9151c171f1d0ca32e8dde06adb812b5d3476e62e Mon Sep 17 00:00:00 2001 From: debangi29 Date: Mon, 10 Jun 2024 03:00:30 +0530 Subject: [PATCH 1/4] Added solution for leetcode 75- sort colors --- .../lc-solutions/0000-0099/0075-sort-colors | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0075-sort-colors diff --git a/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors b/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors new file mode 100644 index 000000000..1d8a787c4 --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors @@ -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 == nums.length +- 1 <= n <= 300 +- nums[i] is either 0, 1, or 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 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& 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. \ No newline at end of file From d16ff4f92ada1500810b2a6c0508dd38af29ec31 Mon Sep 17 00:00:00 2001 From: debangi29 Date: Mon, 10 Jun 2024 03:03:00 +0530 Subject: [PATCH 2/4] Added solution for leetcode 75- sort colors! --- .../0000-0099/{0075-sort-colors => 0075-sort-colors.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename dsa-solutions/lc-solutions/0000-0099/{0075-sort-colors => 0075-sort-colors.md} (100%) diff --git a/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors b/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md similarity index 100% rename from dsa-solutions/lc-solutions/0000-0099/0075-sort-colors rename to dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md From 26f42ddf2064a4bc1cbd94038f4c1e31b89f2aab Mon Sep 17 00:00:00 2001 From: Debangi Ghosh <117537653+debangi29@users.noreply.github.com> Date: Mon, 10 Jun 2024 12:24:33 +0530 Subject: [PATCH 3/4] Updates 0075-sort-colors.md --- dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md b/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md index 1d8a787c4..a85128439 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md +++ b/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md @@ -43,9 +43,9 @@ You must solve this problem without using the library's sort function. ### Constraints -- n == nums.length -- 1 <= n <= 300 -- nums[i] is either 0, 1, or 2. +- $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. @@ -148,4 +148,4 @@ class Solution { 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. \ No newline at end of file +- Space Complexity: $O(1)$ as we are not using any extra space. From 20714ed2edaca7d95cda5195aa9faad26ef9663a Mon Sep 17 00:00:00 2001 From: Debangi Ghosh <117537653+debangi29@users.noreply.github.com> Date: Mon, 10 Jun 2024 23:06:04 +0530 Subject: [PATCH 4/4] Updated 0075-sort-colors.md ! --- dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md b/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md index a85128439..8abd0ee0d 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md +++ b/dsa-solutions/lc-solutions/0000-0099/0075-sort-colors.md @@ -52,7 +52,7 @@ 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. +- 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.