LeetCode problem#75
Sort Colors
Solved in 1 languageJava
Solution
1class Solution {
2 // space O(1)
3 // with zero, index, and two pointers.
4 public void sortColors(int[] nums) {
5 int i = 0;
6 int k = 0;
7 int j = nums.length - 1;
8 while (k <= j) {
9 if (nums[k] == 2) {
10 nums[k] = nums[j];
11 nums[j] = 2;
12 j--;
13 }
14 else {
15 if (nums[k] == 0) {
16 nums[k] = nums[i];
17 nums[i] = 0;
18 i++;
19 }
20 k++;
21 }
22 }
23 }
24
25 // public void sortColors(int[] nums) {
26 // int[] counts = new int[3];
27
28 // for (int i = 0; i < nums.length; i++) {
29 // counts[nums[i]]++;
30 // }
31
32 // int j = 0;
33 // int index = 0;
34 // while (index < nums.length) {
35 // if (counts[j] <= 0) {
36 // j++;
37 // continue;
38 // }
39 // nums[index] = j;
40 // counts[j]--;
41 // index++;
42 // }
43 // }
44}How it works
This Leetcode problem is known as "Sort Colors," where you are asked to sort an array containing integers that represent three different colors. Typically, the integers 0, 1, and 2 are used to represent these colors. The goal is to sort the array in-place, using constant space (O(1)), and in a single pass through the array.
Explanation of the Solution
The provided solution uses a three-pointer technique, which is efficient for sorting these three distinct values (0s, 1s, and 2s). Let’s break down the main parts of the algorithm:
-
Pointers Initialization:
i: This pointer will track the position where the next 0 should be placed.k: This is the current pointer that will traverse through the array.j: This pointer will track the position where the next 2 should be placed.
Initially:
i = 0(starting index for 0s)k = 0(current pointer)j = nums.length - 1(starting index for 2s)
-
Processing the Array:
The main idea is to iterate through the array with thekpointer and useiandjto position 0s and 2s, respectively. We will keep movingkthrough the array until all elements are sorted.The conditions inside the
whileloop are:- If
nums[k] == 2: Swapnums[k]withnums[j], decrementj, and do not incrementksince the new element atk(after the swap) needs to be processed in the next iteration. - If
nums[k] == 0: Swapnums[k]withnums[i], incrementi, and incrementkto move to the next element. This action places a 0 in the appropriate position and movesiforward. - If
nums[k] == 1: Just incrementksince 1s are already in the correct region (between 0s and 2s).
- If
-
Termination:
The loop continues untilkexceedsj. When this happens, all the elements have been sorted in-place.
Time and Space Complexity
- Time Complexity: O(n), where n is the number of elements in the array, because we are only making a single pass through the array.
- Space Complexity: O(1), since we are sorting in-place using a constant amount of additional space (only a few integer variables).
Alternative Approach
The commented-out code presents an alternative approach, which uses counting sort:
- Count the occurrences of each color (0, 1, and 2) using an array
counts. - Then, overwrite the original array with the counted values, based on their counts.
This method also sorts the colors but uses O(n) space for the count array, which is not acceptable for the problem's constraint of O(1) space.
Summary
This solution effectively utilizes the three-pointer technique to classify the colors in a single pass, ensuring efficiency in both time and space. The clear transitions between directing 0s to the front and 2s to the back keeps the algorithm intuitive and performant.
Written by AI from the code above, so double-check anything important.
Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.