lintcode-medium-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.

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?

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;

        int start = 0;
        int end = colors.length - 1;
        int count = 0;

        while(count < k){
            int left = start;
            int right = end;

            int max = Integer.MIN_VALUE;
            int min = Integer.MAX_VALUE;

            for(int i = start; i <= end; i++){
                max = Math.max(max, colors[i]);
                min = Math.min(min, colors[i]);
            }

            while(left < right && colors[left] == min)
                left++;
            while(left < right && colors[right] == max)
                right--;

            int i = left;

            while(i <= right){
                if(colors[i] == min){
                    swap(colors, i, left);
                    left++;

                    while(left < right && colors[left] == min)
                        left++;

                    i = left;
                }
                else if(colors[i] == max){
                    swap(colors, i, right);
                    right--;

                    while(left < right && colors[right] == max)
                        right--;
                }
                else{
                    i++;
                }
            }

            start = left;
            end = right;
            count += 2;
        }

        return;
    }

    public void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;

        return;
    }
}
时间: 2024-11-12 18:52:36

lintcode-medium-Sort Colors II的相关文章

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/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. 注意

Sort Colors II Lintcode

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 pro

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.ExampleGiven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-p

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

LeetCode 75. 颜色分类(Sort Colors) 30

75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. 注意: 不能使用代码库中的排序函数来解决这道题. 每日一算法2019/6/2Day 30LeetCode75. Sort Colors 示例: 输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2] 进阶: 一个直观的解决方

刷题75. Sort Colors

一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是简单的,代码如下: class Solution{ public: void sortColors(vector<int>& nums){ int num0=0,num1=0,num2=0; for(int i=0;i<nums.size();i++){ if(nums[i]==0)

Sort colors系列

75. Sort Colors 问题描述: 给一个包含n个数字的数组,其中有0,1,2:排序使得所有相同的数字相邻,且按照0,1,2的顺序. 思路: (1)计数排序: 需要扫两遍数组,一遍统计个数,第二遍开始摆放数字. 代码如下: 1 void sortColors(vector<int>& nums) { 2 int i=0,j=0,k=0; 3 int n=nums.size(); 4 for(int p=0;p<n;p++) 5 { 6 if(nums[p]==0) 7 i

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

LeetCode OJ平台上Sort Colors题目算法探讨

原题如下,意思就是说无序数组(由0,1,2组成),一遍扫描,把数组整理成若干个0-若干个1-若干个2这样的序列. Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integ