[LeetCode] 75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?

Thought process:
Counting sort, as described in the follow up. In this case, the sorting does not need to be stable, so we can skip the intermediate step of counting sort.

Solution 1:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Solution {
    public void sortColors(int[] nums) {
        int[] counts = new int[3];
        
        for (int num : nums) {
            counts[num]++;
        }
        
        int i = 0;
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < counts[j]; k++) {
                nums[i] = j;
                i++;
            }
        }
    }
}

Time complexity: O(n).

As the follow up says, this solution is rather naive. I need to come up with a one-pass solution using constant space. The idea is to use three pointers. 
  1. Pointer 1 is used to iterate through the array.
  2. Pointer 2 is used to fill the start of the array with 0s.
  3. Pointer 3 is used to fill the end of the array with 2s.
The order of swapping matters. I need to swap 2 to the right before swapping 0 to the left, because there might be 0 at pointer 3. There will never be 2 at pointer 1, because pointer 3 has swapped all 2s to the right. Whatever is left in the middle are all 1s. This is the "one pass" solution.

Solution 2:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
    public void sortColors(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        
        for (int i = 0; i <= right; i++) {
            while (nums[i] == 2 && i < right) {
                nums[i] = nums[right];
                nums[right] = 2;
                right--;
            }
            
            while (nums[i] == 0 && i > left) {
                nums[i] = nums[left];
                nums[left] = 0;
                left++;
            }
        }
    }
}

Be careful about the terminating conditions of the while loops. If i >= right, that means whatever is left at the end of the array is already all 2s. If i <= left, I cannot swap them because that would overwrite the 0s.

Time complexity:
In the worst case, it takes two passes to iterate through the array [2, 2, 0, 0]. The overall time complexity is O(n).

Comments

Popular posts from this blog

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula