【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 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。
/** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...
- iOS开发之删除过期Provisioning Profiles方法
1.在finder下打开go -> go to folder输入: ~/Library/MobileDevice/Provisioning Profiles 2.查看上面的列表,依照时间顺序删除 ...
- 挑战:万能的slash! 判断js中“/”是正则、除号、注释?
很久以前在其它地方就探讨和关注过这个问题,但都没有满意的解答. 看了zjfeihu 的帖子: <前端代码加亮插件(html,jss,css),支持即时加亮,运行代码>,再次提出这个比较经典 ...
- struts.properties文件
此配置文件提供了一种机制来更改默认行为的框架.其实所有的struts.propertiesconfiguration文件中包含的属性也可以被配置在web.xml中使用的init-param,以及在st ...
- Tomcat无法启动:Server Tomcat v8.5 Server at localhost failed to start
Tomcat无法启动 项目状态 Maven项目:基础环境(依赖,基本配置文件)搭建完成,前端页面都导入,部署测试项目环境,出现该问题 问题情景: 1.弹窗提示Tomcat启动失败 2.Console ...
- 转载: vim使用技巧
两篇很牛的vim使用技巧 来源: ChinaUnix博客 日期: 2009.07.06 10:18 (共有条评论) 我要评论 读本文之前请注意:1. 本文的目标是提供一些vim的使用技巧,利用 ...
- 集成 SOLR 到 TOMCAT 中(傻瓜教程)
按照如下配置,整个 Solr 是绿色版的,可以将 Tomcat 目录复制到任何一个地方运行 1.下载 solr 4.3 版本 2.下载 Tomcat 7 ( 6 也可以),另外可以根据系统下载 32 ...
- 数据链路层负载均衡 Linux Virtual Server
w 李智慧
- react-native 中使用redux 优化 Connect 使用装饰器简化代码报错
报错信息 error: bundling failed: Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' opti ...
- js实现文字列表无缝向上滚动
body{font-size:12px} #demo{overflow:hidden; height:80px; width:280px; margin:90px auto; position:rel ...