【HackerRank】 The Full Counting Sort

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.

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.

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?

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.

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.

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.

Input Format
n - the size of the list ar.
n lines follow, each containing an integer x, and a string, s.

Output Format
Print the strings in their correct order.

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 }

 

你可能感兴趣的:(count)