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);
        }
    }
  • 相关阅读:
    详解git pull和git fetch的区别
    什么是移臂调度,什么是旋转调度?
    常用的资源分配策略有哪两种?在每一种策略中,资源请求队列的排序原则是什么?
    什么是虚拟资源,对主存储器而言,用户使用的虚拟资源是什么?
    进程调度的任务是什么,线程调度的任务是什么?
    用于进程控制的原语主要由哪几个,每个原语的执行将使进程的状态发生什么变化?
    试说明进程创建的主要功能是什么?
    什么是线程,线程与进程有什么区别?
    什么是进程互斥,什么是进程同步,同步和互斥这两个概念有什么联系和区别?
    n个并发进程共用一个公共变量Q,写出用信号灯实现n个进程互斥的程序描述,给出信号灯值得取值范围,并说明每个取值范围的物理意义。
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9552188.html
Copyright © 2011-2022 走看看