你的位置:首页 > Java教程

[Java教程]Lintcode: Sort Colors II


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.NoteYou are not suppose to use the library's sort function for this problem.ExampleGIven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4]. ChallengeA 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?

先写了个O(kN)时间复杂度,O(1)空间复杂度的, 这个算法适合颜色数比较少的情况(k is constant)

 1 class Solution { 2   /** 3    * @param colors: A list of integer 4    * @param k: An integer 5    * @return: nothing 6   */ 7   public void sortColors2(int[] colors, int k) { 8     // write your code here 9     if (colors==null || colors.length==0 || k<=1) return;10     int l=0, r=colors.length-1;11     int pivot = r;12       while (true) {13         while (l<r && colors[l]<colors[pivot]) {14           r--;15         }16         while (l<r && colors[l]==cnt) {17           l++;18         }19         if (l == r) break;20         swap(colors, l, r);21       }22       l++;23       r = colors.length-1;24       if (l == r) break;25   }26   27   public void swap(int[] colors, int l, int r) {28     int temp = colors[l];29     colors[l] = colors[r];30     colors[r] = temp;31   }32 }

K->N的话,上面时间复杂度就大了,所以干脆用Quick Sort

 1 class Solution { 2   /** 3    * @param colors: A list of integer 4    * @param k: An integer 5    * @return: nothing 6   */ 7   public void sortColors2(int[] colors, int k) { 8     // write your code here 9     if (colors==null || colors.length==0 || k<=1) return;10     quickSort(colors, 0, colors.length-1);11   }12   13   public void quickSort(int[] colors, int l, int r) {14     if (l >= r) return;15     int pivot = r;16     int pos = partition(colors, l, r, pivot);17     quickSort(colors, l, pos-1);18     quickSort(colors, pos+1, r);19   }20   21   public int partition(int[] colors, int start, int end, int pivot) {22     int l=start, r=end;23     while (true) {24       while (l<r && colors[l]<colors[pivot]) {25         l++;26       }27       while (l<r && colors[r]>=colors[pivot]) {28         r--;29       }30       if (l == r) break;31       swap(colors, l, r);32     }33     swap(colors, l, end);34     return l;35   }36   37   public void swap(int[] colors, int l, int r) {38     int temp = colors[l];39     colors[l] = colors[r];40     colors[r] = temp;41   }42 }

 网上有人给出了O(N)的解法,没懂

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

 1 // Solution 2: inplace, O(n)  2   public void sortKColors(int[] colors, int k) { 3     // write your code here 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     }