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.

Note

You are not suppose to use the library‘s sort function for this problem.

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?

Analysis:

Sort: O(nlogn), quick or merge.

O(n): use the array itself as space to store counts. We use A[k-1] to store the count of color k. We use negtive number to store count, in order to be distnct with the color value. This method ASSUMES that every color between 1 and k will appear.

At position i, we check the value of A[A[i]-1], if it is a positive number, i.e., not counted yet, we then put A[A[i]-1] to A[i], and set A[A[i]-1] as -1 to indicate that there is one of this color.

If A[A[i]-1] is a negtive or zero value, we then simply decrease it by one and set A[i] as 0 to indicate that this position is couted already.

At position i, we repeat this procedure until A[i] becomes 0 or negtive, we then move to i+1.

At counting, we draw colors into array.

Solution:

 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         //The method assumes that every color much appear in the array.
 9         int len = colors.length;
10         if (len<k) return;
11
12         //count the number of each color.
13         for (int i=0;i<len;i++){
14             while (colors[i]>0){
15                 int key = colors[i]-1;
16                 if (colors[key]<=0){
17                     colors[key]--;
18                     colors[i]=0;
19                 } else {
20                     colors[i] = colors[key];
21                     colors[key] = -1;
22                 }
23             }
24         }
25
26         //draw colors.
27         int index = len-1;
28         int cInd = k-1;
29         while (cInd>=0){
30             int num = Math.abs(colors[cInd]);
31             for (int i=0;i<num;i++){
32                 colors[index]=cInd+1;
33                 index--;
34             }
35             cInd--;
36         }
37     }
38 }

时间: 2024-10-03 22:50:01

LintCode-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] Sort Integers II 整数排序之二

Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm. Example Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5]. 解法一: // Quick sort class Solution { public: /** * @param A an integer array *

LintCode: Sort Colors

通过交换,对0,1,2排序 使用三个标记[循环不变式] i从前向后,记录最后一个0的位置 j从后向前,记录第一个2的位置 k从前向后,是遍历用的游标 [0..i-1]是0 [i..k-1]是1 [k,j-1]是未探测 [j..n-1]是2 初始k=0时,0,1,2的区域都是空,所有区域都是未探测,循环k=0..n-1 如果a[k] = 0=>swap(a[i++], a[k]) 如果a[k] = 1=>无操作 如果a[k] = 2=>swap(a[--j], a[k--]) 复杂度O(n

lintcode:排颜色 II

排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 样例 给出colors=[3, 2, 2, 1, 4],k=4, 你的代码应该在原地操作使得数组变成[1, 2, 2, 3, 4] 解题 直接快排 class Solution { /** * @param colors: A list of integer * @param k: An integer * @return: nothin

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] 进阶: 一个直观的解决方

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