zoukankan      html  css  js  c++  java
  • lintcode143

    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);
        }
    }
  • 相关阅读:
    什么是单例模式?
    下拉列表(web),用jQuery实现
    PS基础
    C#笔记
    C#基础篇--文件(流)
    C#基础篇03
    今天自学了网页上注册某某时的倒计时设置
    主数据管理的Jill Dyche, Evan Levy六层次模型
    scala akka 修炼之路6(scala函数式柯里化风格应用场景分析)
    OpenWrt mt7620n双SSID实现原理
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9552188.html
Copyright © 2011-2022 走看看