[Hackerrank] the full counting sort

Source: Internet
Author: User

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 shocould beStable, I. e. the original order shocould 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 ListAr.
NLines follow, each containing an integerX, 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 stringSIs in lowercase.

 

Question:

Given a string (integer, string) sequence, sort these tuples according to the integer size, and then output the words that start in the half of the array according to the sorted words, the word in the first half is replaced.

An example is:

Sample Input200 ab6 cd0 ef6 gh4 ij0 ab6 cd0 ef6 gh0 ij4 that3 be0 to1 be5 question1 or2 not4 is2 to4 theSample Output- - - - - to be or not to be - that is the question - - - -

There are two very important information here. One is the position of the string and integer corresponding to the integer in the original array.

Therefore, two maps are used:

HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();HashMap<Integer, ArrayList<Integer>> index_Map = new HashMap<Integer, ArrayList<Integer>>();

One is used to record the index of the first integer in the original array, and the other is used to record the string corresponding to the first integer in the tuples. For example, in map, the list corresponding to 0 is <AB, EF, AB, EF, IJ, to>. In index_map, the list corresponding to 0 is <0, 2, 5, 7, 9, 12>.

In this way, the result is output directly after two hashmaps are obtained. For example, a string corresponding to 0 is output based on two hashmaps. The index of the first 0 string in the original array is 0, which belongs to the first half, so it is replaced, 0 corresponds to the last string "to". The index in the original array is 12, which belongs to the second half. Therefore, it should be output. Then, processing 1, 2, 3... the corresponding string is returned.

The Code is as follows:

 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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.