In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the original order should be maintained for equal elements.java
Insertion Sort and the simple version of QuickSort were stable, but the faster in-place version of Quicksort was not (since it scrambled around elements while sorting).數組
In cases where you care about the original order, it is important to use a stable sorting algorithm. In this challenge, you will use counting sort to sort a list while keeping the order of the strings (with same accompanying integer) preserved.app
Challenge
In the previous challenge, you created a "helper array" that contains information about the starting position of each element in a sorted array. Can you use this array to help you create a sorted array of the original list? ui
Hint: You can go through the original array to access the strings. You can then use your helper array to help determine where to place those strings in the sorted array. Be careful about being one off. this
Details and a Twist
You will be given a list that contains both integers and strings. Can you print the strings in order of their accompanying integers? If the integers for two strings are equal, ensure that they are print in the order they appeared in the original list. spa
The Twist - Your clients just called with an update. They don't want you to print the first half of the original array. Instead, they want you to print a dash for any element from the first half. So you can modify your counting sort algorithm to sort the second half of the array only. code
Input Format
n - the size of the list ar.
n lines follow, each containing an integer x, and a string, s.orm
Output Format
Print the strings in their correct order. blog
Constraints
1 <= n <= 1000000
n is even
1 <= length(s) <= 10
0 <= x < 100 , x ∈ ar
The characters in every string s is in lowercase.排序
題解:
給出一串(整數,字符串)序列,要求根據整數的大小排序這些元組,而後把開始位於數組後半部分的單詞按照排序後的單詞輸出,位於前半部分的單詞則用「-」代替。
一個例子是:
Sample Input 20 0 ab 6 cd 0 ef 6 gh 4 ij 0 ab 6 cd 0 ef 6 gh 0 ij 4 that 3 be 0 to 1 be 5 question 1 or 2 not 4 is 2 to 4 the Sample Output - - - - - to be or not to be - that is the question - - - -
這裏有兩個信息十分重要,一個是整數對應的字符串和整數在原數組中的位置。
因此用兩個map:
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>(); HashMap<Integer, ArrayList<Integer>> index_Map = new HashMap<Integer, ArrayList<Integer>>();
一個用來記錄元組中第一個整數在原數組中的索引,一個用來記錄元組中第一個整數對應的字符串。好比在map中,0對應的list是<ab,ef,ab,ef,ij,to>,在index_Map中,0對應的list是<0,2,5,7,9,12>。
這樣就能夠在獲得兩個hashmap後直接輸出結果了。好比先根據兩個hashmap輸出0對應的串,第一個0對應的串在原數組中的索引是0,屬於前半部分,因此它用"-"代替,0對應的最後一個串「to「在原數組中的索引是12,屬於後半部分,因此它應該輸出;依次再處理1,2,3...對應的串,就獲得結果了。
代碼以下:
1 import java.io.*; 2 import java.util.*; 3 4 public class Solution { 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 int s = in.nextInt(); 8 HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>(); 9 HashMap<Integer, ArrayList<Integer>> index_Map = new HashMap<Integer, ArrayList<Integer>>(); 10 int[] count = new int[100]; 11 for(int i=0;i<s;i++){ 12 int key = in.nextInt(); 13 count[key]++; 14 String value = in.next(); 15 if(!map.containsKey(key)){ 16 map.put(key, new ArrayList<String>()); 17 index_Map.put(key, new ArrayList<Integer>()); 18 } 19 index_Map.get(key).add(i); 20 map.get(key).add(value); 21 } 22 23 int mid = s/2; 24 StringBuffer sb = new StringBuffer(); 25 for(int i = 0;i < 100;i++ ) 26 { 27 if(map.containsKey(i)){ 28 for(int j = 0;j < map.get(i).size();j++){ 29 int index = index_Map.get(i).get(j); 30 String string = map.get(i).get(j); 31 if(index < mid) 32 sb.append("-").append(" "); 33 else 34 sb.append(string).append(" "); 35 } 36 } 37 } 38 System.out.println(sb.toString()); 39 } 40 }