lintcode143 - Sort Colors II - medium

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.
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?
Notice
You are not suppose to use the library's sort function for this problem.
k <= n

 

1.O(nk)方法。
每一轮,扫描一次找到最小色和最大色,再扫描一次把最小色放到left++,最大色放到right—。
重复k/2轮就排好了

2.O(nlogk)方法,极限。
快排思想的递归。每次把中间颜色都放到最中间那块地盘,保证左边的都是比中间色小的,右边的都是比中间色大的,左右两块内部顺序随意,之后递归排着。每一次扫描一整遍O(n),一共要这样调用logk层。因为每次是对颜色这个对象二分规模,所以是logk。
每一轮具体也是两根指针往中间缩,left指到不合格的>mid的对象,right指到不合格的<=mid的对象,就停下来互换。
细节:
1.因为midColor=()/2是不对称的,会偏小,所以对比colors[left] colors[right]和midColor也不对称。想最极端的只有两色12的情况,mid是1,left要指到2这种>1的才能停,right要指到<=1的都要停。上面加=减=你试试就知道都不行,要么把位置对的换走了,要么根本指不到。
2.要解决已经排好序了从头到位只有一根指针在移动的情况,也就是把n个元素进一步分为n个的组和0个的组,那你再递归下去就死循环了。所以如果发现有一根指针没动过,那就不继续递归了。

 

1.O(nk)实现

public class Solution {
    /**
     * @param colors: A list of integer
     * @param k: An integer
     * @return: nothing
     */
    public void sortColors2(int[] colors, int k) {
        // write your code here
        
        int left = 0, right = colors.length - 1;
        int count = 0;
        while (count < k) {
            int min = k, max = 1;
            for (int i = left; i <= right; i++) {
                min = Math.min(min, colors[i]);
                max = Math.max(max, colors[i]);
            }
            int ptr = left;
            while (ptr <= right) {
                if (colors[ptr] == min) {
                    swap(colors, left, ptr);
                    left++;
                    ptr++;
                } else if (colors[ptr] == max) {
                    swap(colors, right, ptr);
                    right--;
                } else {
                    ptr++;
                }
            }
            count+=2;
        }
    }
    
    private void swap(int[] colors, int i, int j) {
        int temp = colors[i];
        colors[i] = colors[j];
        colors[j] = temp;
    }
}

 

 

2.O(nlogk)实现

public class Solution {
    /**
     * @param colors: A list of integer
     * @param k: An integer
     * @return: nothing
     */
    public void sortColors2(int[] colors, int k) {
        // write your code here
        if (colors == null || colors.length == 0) {
            return;
        }
        partitionColors(colors, 0, colors.length - 1, 1, k);
    }
    
    private void partitionColors(int[] colors, int start, int end, int startColor, int endColor) {
        
        if (start >= end || startColor >= endColor) {
            return;
        }
        
        int left = start, right = end;
        int midColor = (startColor + endColor) / 2;
        while (left < right) {
            // 注意不是<,因为mid求的时候有偏小。写错对只有两色的情况如21122就会把正确的1换走了
            while (left < right && colors[left] <= midColor) {
                left++;
            }
            while (left < right && colors[right] > midColor) {
                right--;
            }
            if (left < right) {
                int temp = colors[left];
                colors[left] = colors[right];
                colors[right] = temp;
                left++;
                right--;
            }
        }
        if (start == left || end == right) {
            return;
        }
        partitionColors(colors, start, right, startColor, midColor);
        partitionColors(colors, left, end, midColor, endColor);
    }
}

 

转载于:https://www.cnblogs.com/jasminemzy/p/9552188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值