Lintcode: Sort Colors II Problem Solving report
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 that 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 shocould sort colors in-place to [1, 2, 2, 3, 4]. challenge A rather straight forward solution is a two-pass algo Rithm using counting sort. That will cost O (k) extra memory. Can you do it without using extra memory? SOLUTION 1: Use a fast sort. the time complexity is O (nlogn), and the space complexity is O (1) 1/** 2 * @ param colors: A list of integer 3 * @ param k: An integer 4 * @ return: nothing 5 */6/* 7 Solution 1: Using the quick sort. 8 */9 public void sortKColors1 (int [] colors, int k) {10 // write your code here11 if (colors = null) {12 return; 13} 14 15 quickSort (colors, 0, colors. length-1); 16} 17 18 public void quickSort (int [] colors, int left, in T right) {19 if (left> = right) {20 return; 21} 22 23 int partition = colors [right]; 24 25 int pos = partition (colors, left, right, rows); 26 27 quickSort (colors, left, pos-1); 28 quickSort (colors, pos + 1, right ); 29} 30 31 public int partition (int [] colors, int left, int right, int trim) {32 int leftPoint = left-1; 33 int rightPoint = right; 34 35 while (true) {36 while (colors [++ leftPoint] <strong ); 37 38 while (leftPoint <rightPoint & colors [-- rightPoint]> priority); 39 40 if (leftPoint> = rightPoint) {41 break; 42} 43 44 swap (colors, leftPoint, rightPoint); 45} 46 47 swap (colors, leftPoint, right); 48 return leftPoint; 49} 50 51 public void swap (int [] colors, int left, int right) {52 int tmp = colors [left]; 53 colors [left] = colors [right]; 54 colors [right] = tmp; 55} SOLUTION 2: inplace, and O (N) Time is complex Degree algorithm. We can use the bucket sorting idea to count all the numbers. 1. scan from left to right. When you encounter a number, first find the corresponding bucket. for example, 3 2 2 1 4 the bucket corresponding to the first 3 is index = 2 (the bucket starts from 0) 2. if the Bucket has a number, move the number to the position (that is, to store it) of I, and then mark the bucket as-1 (indicating that the position is a counter, counted as 1 ). 3. the Bucket stores a negative number, indicating that the bucket is already a counter and is directly reduced by 1. set color [I] to 0, which indicates that the color has been calculated. 4. the Bucket stores 0, which is the same as 3. Set the bucket to-1, and set color [I] to 0 (indicating that the Bucket has been computed here. return to position I, and then judge whether the value is 0 (as long as the value is not 0, repeat steps 2-4 ). 6. After completing steps 1-5, the array is set from the end to the header. (From the end to the header to avoid overwrite the counter at the beginning) Example (perform the operation following the preceding steps ): 3 2 2 1 4 2 2-1 1 4 2-1 1 1 4 0-2-1 1 4-1 1 1-1-2-1 0 4-1-2-1- 1 0 1 // Solution 2: inplace, O (n) 2 public void sortKColors (int [] colors, int k) {3 // write your code here 4 if (colors = null) {5 return; 6} 7 8 int len = colors. length; 9 for (int I = 0; I <len; I ++) {10 // Means need to deal with A [I] 11 while (colors [I]> 0) {12 int num = colors [I]; 13 if (colors [num-1]> 0) {14 // 1. there is a number in the bucket, 15 // Store the number in the bucket in position I; 16 colors [I] = colors [num-1]; 17 colors [num-1] =-1; 18} else if (colors [num-1] <= 0) {19 // 2. bucket is using or the bucket is empty.20 colors [num-1] --; 21 // delete the [I]; 22 colors [I] = 0; 23} 24} 25} 26 27 int index = len-1; 28 for (int I = k-1; I> = 0; I --) {29 int cnt =-colors [I]; 30 31 // Empty number.32 if (cnt = 0) {33 continue; 34} 35 36 while (cnt> 0) {37 colors [index --] = I + 1; 38 cnt --; 39} 40}