Skip to content

Commit

Permalink
SortColors I && II
Browse files Browse the repository at this point in the history
Change-Id: I333d915b135f867703e379f4c61fe80bbc29c088
  • Loading branch information
applewjg committed Dec 29, 2014
1 parent 81e067a commit f81c713
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
38 changes: 38 additions & 0 deletions SortColors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Author: King, [email protected]
Date: Dec 24, 2013
Problem: Sort Colors
Difficulty: Medium
Source: https://oj.leetcode.com/problems/sort-colors/
Notes:
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?
Solution: 0 0 0 1 1 1 1 ...... 2 2 2 2
| | |
zero i two
-> -> <-
*/
public class Solution {
public void sortColors(int[] A) {
int n = A.length;
if (n <= 1) return;
for (int i = 0, left = 0, right = n - 1; i <= right;) {
if (A[i] == 0) {
A[i++] = A[left];
A[left++] = 0;
} else if (A[i] == 2) {
A[i] = A[right];
A[right--] = 2;
} else i++;
}
}
}
54 changes: 54 additions & 0 deletions SortColorsII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Author: King, [email protected]
Date: Dec 24, 2013
Problem: Sort Colors
Difficulty: Medium
Source: http://lintcode.com/zh-cn/problem/sort-colors-ii/
Notes:
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.
Note
You are not suppose to use the library's sort function for this problem.
Example
GIven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4].
Challenge
A rather straight forward solution is a two-pass algorithm using counting sort. That will cost O(k) extra memory.
Can you do it without using extra memory?
Solution: Use the first k buckets to store the count. (count sort, two pass).
*/
class Solution {
/**
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] A, int k) {
// write your code here
int n = A.length;
if (n <= 1) return;
for (int i = 0; i < n; ++i) {
if(A[i] > 0) {
int c = A[i];
A[i] = 0;
while(true) {
if (A[c-1] <= 0) {
--A[c-1];
break;
} else {
int col = A[c-1];
A[c-1] = -1;
c = col;
}
}
}
}
int idx = n;
for (int i = k; i > 0; --i) {
for (int j = 0; j > A[i-1]; --j) A[--idx] = i;
}
}
}

0 comments on commit f81c713

Please sign in to comment.