【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...对应的串,就得到结果了。
代码如下:
import java.io.*;
import java.util.*; public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
HashMap<Integer, ArrayList<Integer>> index_Map = new HashMap<Integer, ArrayList<Integer>>();
int[] count = new int[100];
for(int i=0;i<s;i++){
int key = in.nextInt();
count[key]++;
String value = in.next();
if(!map.containsKey(key)){
map.put(key, new ArrayList<String>());
index_Map.put(key, new ArrayList<Integer>());
}
index_Map.get(key).add(i);
map.get(key).add(value);
} int mid = s/2;
StringBuffer sb = new StringBuffer();
for(int i = 0;i < 100;i++ )
{
if(map.containsKey(i)){
for(int j = 0;j < map.get(i).size();j++){
int index = index_Map.get(i).get(j);
String string = map.get(i).get(j);
if(index < mid)
sb.append("-").append(" ");
else
sb.append(string).append(" ");
}
}
}
System.out.println(sb.toString());
}
}
【HackerRank】 The Full Counting Sort的更多相关文章
- 【Atcoer】ARC088 E - Papple Sort
[题目]E - Papple Sort [题意]给定长度为n的小写字母串,只能交换相邻字母,求形成回文串的最小步数.n<=2*10^5. [算法]数学 [题解]官方题解 本题题解中有以下重要的思 ...
- 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)
Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...
- 【转】 std list/vector sort 排序
[转自]http://blog.csdn.net/marising/article/details/4567531 网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多.关于排序,我还真没研究过,看了江湖 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【转载】双调排序Bitonic Sort,适合并行计算的排序算法
双调排序是data-independent的排序, 即比较顺序与数据无关的排序方法, 特别适合做并行计算,例如用GPU.fpga来计算. 1.双调序列 在了解双调排序算法之前,我们先来看看什么是双调序 ...
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HDOJ】4358 Boring counting
基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...
- 【转】linux中的sort命令
转自:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分 ...
- 【转载】C#中自定义Sort的排序规则IComparable接口
C#中的List集合在排序的时候,如果不使用Lambda表达式进行排序的话,一般调用Sort()方法进行排序,如果希望Sort()方法排序后的结果跟我们预想的效果一致或者按照我们自定义的规则排序,则需 ...
随机推荐
- 2017-5-14 湘潭市赛 Highway 先获得直径S,T。则一开始S,T相连,然后其他的点如果离S更远那么连在S,否则T;
Highway Accepted : Submit : Time Limit : MS Memory Limit : KB Highway In ICPCCamp there were n towns ...
- hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...
- 大数据(5) - HDFS中的常用API操作
一.安装java 二.IntelliJ IDEA(2018)安装和破解与初期配置 参考链接 1.进入官网下载IntelliJ IDEA https://www.jetbrains.com/idea/d ...
- MAC下一些常用的命令行
统计了一下工作中一些会常用到的简单命令,加强记忆 ls 查看当前终端目录下面的文件 ls -a "ls -a"会出现一些带.xxxx的文 ...
- python 爬虫5 Beautiful Soup的用法
1.创建 Beautiful Soup 对象 from bs4 import BeautifulSoup html = """ <html><head& ...
- iframe详解
如何查看是否为iframe *使用FireFox组件firebug->firepath 1.Top Window:可直接定位 2.iframe#i:根据id定位 定位方法: switch_to. ...
- POJ 2479 Maximum sum(双向DP)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36100 Accepted: 11213 Des ...
- jqgrid 事件说明
Events(事件) 事件响应动作被设置为表格的属性,以下定义了行被选中时的响应: var lastSel; jQuery("#gridid").jqGrid({ ... o ...
- filter和find区别,元素遍历
转 filter和find区别 find()会在当前指定元素中查找符合条件的子元素,是对它的子集操作,而filter()则是在当前指定的元素集合中查找符合条件的元素,是对自身集合元素进行筛选. HTM ...
- 路径 php中'.'和'..'还有'./'和'../'
./当前目录(就是当前执行文件所在目录) ../上级目录 / 这个才是根目文件名/ 同级目录 例子如图 1.cart下的index.php 1)要引用Public->css->index. ...