Lintcode: Sort Colors II 解题报告 - Yu's Garden - 博客园
用3-way-partition的方法,每次把比k小的数字放到前面,把比k大的数字放到后面。
然后递归去排比k小的数字和比k大的数字。
时间复杂度是O(nlogk)。
// version 1: O(nlogk), the best algorithm based on comparing
简单的办法可以利用two pass, 相当于counting sort,计数每个color的个数,再依次填入到原来的array中;这种方法空间复杂度为 O(k), 时间复杂度为 O(n)。
Count sort, 扫两遍即可,需要O(k)的空间
用two pointers办法,每次只能sort出两端的两个数字,因此复杂度比较高O(k/2*n)
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.
Notice
You are not suppose to use the library's sort function for this problem.
k <= n
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?
https://xuezhashuati.blogspot.com/2017/04/lintcode-143-sort-colors-ii.htmlNotice
You are not suppose to use the library's sort function for this problem.
k <= n
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?
用3-way-partition的方法,每次把比k小的数字放到前面,把比k大的数字放到后面。
然后递归去排比k小的数字和比k大的数字。
时间复杂度是O(nlogk)。
public void sortColors2(int[] colors, int k) { if (colors == null || colors.length == 0) { return ; } partition(colors, 0, colors.length - 1, 1, k); } public void partition(int nums[], int lo, int hi, int lowNum, int hiNum) { if (lowNum == hiNum) { return; } if (lo >= hi) { return; } int lt = lo; int gt = hi; int v = (lowNum + hiNum) / 2; int i = lt; while (i <= gt) { if (nums[i] < v) { exch(nums, lt++, i++); } else if (nums[i] > v) { exch(nums, i, gt--); } else { i++; } } partition(nums, lo, lt - 1, lowNum, v - 1); partition(nums, gt, hi, v + 1, hiNum); } public void exch(int[] nums, int x, int y) { int tmp = nums[x]; nums[x] = nums[y]; nums[y] = tmp; }http://www.jiuzhang.com/solutions/sort-colors-ii/
// version 1: O(nlogk), the best algorithm based on comparing
public void sortColors2(int[] colors, int k) { if (colors == null || colors.length == 0) { return; } rainbowSort(colors, 0, colors.length - 1, 1, k); } public void rainbowSort(int[] colors, int left, int right, int colorFrom, int colorTo) { if (colorFrom == colorTo) { return; } if (left >= right) { return; } int colorMid = (colorFrom + colorTo) / 2; int l = left, r = right; while (l <= r) { while (l <= r && colors[l] <= colorMid) { l++; } while (l <= r && colors[r] > colorMid) { r--; } if (l <= r) { int temp = colors[l]; colors[l] = colors[r]; colors[r] = temp; l++; r--; } } rainbowSort(colors, left, r, colorFrom, colorMid); rainbowSort(colors, l, right, colorMid + 1, colorTo); }
// version 2: O(nk), not efficient, will get Time Limit Exceeded error. But you should try to implement the following algorithm for practicing purpose. public void sortColors2(int[] colors, int k) { int count = 0; int start = 0; int end = colors.length - 1; while (count < k) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i = start; i <= end; i++) { min = Math.min(min, colors[i]); max = Math.max(max, colors[i]); } int left = start; int right = end; int cur = left; while(cur <= right) { if (colors[cur] == min) { swap(left, cur, colors); cur++; left++; } else if (colors[cur] > min && colors[cur] < max) { cur++; } else { int tmp = colors[cur]; swap(cur, right, colors); right--; } } count += 2; start = left; end = right; } } void swap(int left, int right, int[] colors) { int tmp = colors[left]; colors[left] = colors[right]; colors[right] = tmp; }https://codesolutiony.wordpress.com/2015/05/28/lintcode-sort-colors-ii/
简单的办法可以利用two pass, 相当于counting sort,计数每个color的个数,再依次填入到原来的array中;这种方法空间复杂度为 O(k), 时间复杂度为 O(n)。
Count sort, 扫两遍即可,需要O(k)的空间
public void sortColors2(int[] colors, int k) {
int[] count = new int[k];
for (int color : colors) {
count[color-1]++;
}
int index = 0;
for (int i = 0; i < k; i++) {
while (count[i]>0) {
colors[index++] = i+1;
count[i]--;
}
}
}
public void sortColors2(int[] colors, int k) {
int start = -1;
int end = colors.length;
for (int round = 1; round*2 <= k; round++) {
int leftColor = round;
int rightColor = k - round + 1;
for (int index = start+1; index < end; index++) {
if (colors[index] == leftColor) {
colors[index] = colors[++start];
colors[start] = leftColor;
} else if (colors[index] == rightColor) {
colors[index] = colors[--end];
colors[end] = rightColor;
index--;
}
}
}
}
使用快排,时间复杂度是O(nlogn),空间复杂度是O(Log(N))
9 public void sortKColors1(int[] colors, int k) { 10 // write your code here 11 if (colors == null) { 12 return; 13 } 14 15 quickSort(colors, 0, colors.length - 1); 16 } 17 18 public void quickSort(int[] colors, int left, int right) { 19 if (left >= right) { 20 return; 21 } 22 23 int pivot = colors[right]; 24 25 int pos = partition(colors, left, right, pivot); 26 27 quickSort(colors, left, pos - 1); 28 quickSort(colors, pos + 1, right); 29 } 30 31 public int partition(int[] colors, int left, int right, int pivot) { 32 int leftPoint = left - 1; 33 int rightPoint = right; 34 35 while (true) { 36 while (colors[++leftPoint] < pivot); 37 38 while (leftPoint < rightPoint && colors[--rightPoint] > pivot); 39 40 if (leftPoint >= rightPoint) { 41 break; 42 } 43 44 swap(colors, leftPoint, rightPoint); 45 } 46 47 swap(colors, leftPoint, right); 48 return leftPoint; 49 }
Doing quick sort partition for K -1 times.
1. Use K - 1 value as pivot
2. Starting from 0, whenever low<high && less or equal to pivot, low++
3. starting from end, whenever high >0, and greater than pivot, high--
4. Result: only swap when low and high have disagreement on the pivot value.
public void sortColors2(int[] colors, int k) {
if (colors == null || colors.length == 0 || k <= 0) {
return;
}
int end = colors.length - 1;
for (int i = 0; i < k - 1; i++) {
end = helper(colors, 0, end, k - i - 1);
}
}
public int helper(int[] colors, int start, int end, int pivot) {
int low = start;
int high = end;
while (low <= high) {
while(low < high && colors[low] <= pivot) {
low++;
}
while(high > 0 && colors[high] > pivot) {
high--;
}
if (low <= high) {
swap(colors, low, high);
low++;
high--;
}
}
return low - 1;
}
X. http://www.jianshu.com/p/9bad070d7802
- 这道题原数组还要重复利用作为bucket数组!!! 首先for循环遍历一遍原来的数组,如果扫到a[i],首先检查a[a[i]]是否为正数,如果是把a[a[i]]移动a[i]存放起来,然后把a[a[i]]记为-1(表示该位置是一个计数器,计1)。 如果a[a[i]]是负数,那么说明这一个地方曾经已经计数了,那么把a[a[i]]计数减一,并把color[i] 设置为0 (表示此处已经计算过),然后重复向下遍历下一个数,这样遍历原数组所有的元素过后,数组a里面实际上存储的每种颜色的计数,然后我们倒着再输出每种颜色就可以得到我们排序后的数组。
例子(按以上步骤运算):
3 2 2 1 4
2 2 -1 1 4
2 -1 -1 1 4
0 -2 -1 1 4
-1 -2 -1 0 4
-1 -2 -1 -1 0
def sortColors2(self, colors, k):
if not colors:
return
for i in range(len(colors)):
while colors[i] > 0:
num = colors[i]
if colors[num - 1] > 0:
colors[i] = colors[num - 1]
colors[num - 1] = -1
elif colors[num - 1] <= 0:
colors[num - 1] -= 1
colors[i] = 0
index = len(colors) - 1
for i in range(k - 1, -1, -1):
temp = colors[i]
while temp < 0:
colors[index] = i + 1
temp += 1
index -= 1
inplace,并且O(N)时间复杂度的算法。
我们可以使用类似桶排序的思想,对所有的数进行计数。
1. 从左扫描到右边,遇到一个数字,先找到对应的bucket.比如
3 2 2 1 4
第一个3对应的bucket是index = 2 (bucket从0开始计算)
2. Bucket 如果有数字,则把这个数字移动到i的position(就是存放起来),然后把bucket记为-1(表示该位置是一个计数器,计1)。
3. Bucket 存的是负数,表示这个bucket已经是计数器,直接减1. 并把color[i] 设置为0 (表示此处已经计算过)
4. Bucket 存的是0,与3一样处理,将bucket设置为-1, 并把color[i] 设置为0 (表示此处已经计算过)
5. 回到position i,再判断此处是否为0(只要不是为0,就一直重复2-4的步骤)。
6.完成1-5的步骤后,从尾部到头部将数组置结果。(从尾至头是为了避免开头的计数器被覆盖)
例子(按以上步骤运算):
3 2 2 1 4
2 2 -1 1 4
2 -1 -1 1 4
0 -2 -1 1 4
-1 -2 -1 0 4
-1 -2 -1 -1 0
2 public void sortKColors(int[] colors, int k) { 4 if (colors == null) { 5 return; 6 } 7 8 int len = colors.length; 9 for (int i = 0; i < len; i++) { 10 // Means need to deal with A[i] 11 while (colors[i] > 0) { 12 int num = colors[i]; 13 if (colors[num - 1] > 0) { 14 // 1. There is a number in the bucket, 15 // Store the number in the bucket in position i; 16 colors[i] = colors[num - 1]; 17 colors[num - 1] = -1; 18 } else if (colors[num - 1] <= 0) { 19 // 2. Bucket is using or the bucket is empty. 20 colors[num - 1]--; 21 // delete the A[i]; 22 colors[i] = 0; 23 } 24 } 25 } 26 27 int index = len - 1; 28 for (int i = k - 1; i >= 0; i--) { 29 int cnt = -colors[i]; 30 31 // Empty number. 32 if (cnt == 0) { 33 continue; 34 } 35 36 while (cnt > 0) { 37 colors[index--] = i + 1; 38 cnt--; 39 } 40 }
http://blog.csdn.net/nicaishibiantai/article/details/43306431
为了省空间,只能花时间了,还是按照sort color的方法,找到当前array中min, max,其余忽略,然后把min/max放到最前和最后。循环直到min/max count = k
Read full article from Lintcode: Sort Colors II 解题报告 - Yu's Garden - 博客园
为了省空间,只能花时间了,还是按照sort color的方法,找到当前array中min, max,其余忽略,然后把min/max放到最前和最后。循环直到min/max count = k
复杂度是O(n^2): T(n) = T(n-2) + n
public void sortColors2(int[] colors, int k) { int count = 0; int start = 0; int end = colors.length-1; while (count < k) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i = start; i < end; i++) { min = Math.min(min, colors[i]); max = Math.max(max, colors[i]); } int left = start; int right = end; int cur = left; while(cur <= right) { if (colors[cur] == min) { swap(left, cur, colors); cur++; left++; } else if (colors[cur] > min && colors[cur] < max) { cur++; } else { int tmp = colors[cur]; swap(cur, right, colors); right--; } } count += 2; start = left; end = right; } }https://github.com/shawnfan/LintCode/blob/master/Java/Sort%20Colors%20II.java
Read full article from Lintcode: Sort Colors II 解题报告 - Yu's Garden - 博客园