[hackerrank]Counting Sort 3
2014-05-16 21:58
411 查看
In the previous challenge, it was easy to print all the integers in order, since you did not have to access the original list. Once you had obtained the frequencies of all the integers, you could simply print each integer in order the correct number of times.
However, if there is other data associated with an element, you will have to access the original element itself.
In the final counting sort challenge, you are required to print the data associated with each integer. This means, you will go through the original array to get the data, and then use some “helper arrays” to determine where to place everything in a sorted array.
If you know the frequencies of each element, you know how many times to place it, but which index will you start placing it from? It will be helpful to create a helper array for the “starting values” of each element.
Challenge
You will be given a list that contains both integers and strings. In this challenge you just care about the integers. For every value i from 0 to 99,
can you output L, the number of elements that are less than or equal to i?
Input Format
n - the size of the list ar.
n lines follow, each containing an integer x,
and a string, s.
Output Format
Output L for all numbers from 0 to 99 (inclusive).
Constraints
1 <= n <= 1000000
0 <= x < 100 , x ∈ ar
Sample Input
Sample Output
However, if there is other data associated with an element, you will have to access the original element itself.
In the final counting sort challenge, you are required to print the data associated with each integer. This means, you will go through the original array to get the data, and then use some “helper arrays” to determine where to place everything in a sorted array.
If you know the frequencies of each element, you know how many times to place it, but which index will you start placing it from? It will be helpful to create a helper array for the “starting values” of each element.
Challenge
You will be given a list that contains both integers and strings. In this challenge you just care about the integers. For every value i from 0 to 99,
can you output L, the number of elements that are less than or equal to i?
Input Format
n - the size of the list ar.
n lines follow, each containing an integer x,
and a string, s.
Output Format
Output L for all numbers from 0 to 99 (inclusive).
Constraints
1 <= n <= 1000000
0 <= x < 100 , x ∈ ar
Sample Input
10 4 that 3 be 0 to 1 be 5 question 1 or 2 not 4 is 2 to 4 the
Sample Output
1 3 5 6 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
n = input() s=[] r = [0 for x in xrange(100)] for _ in range(n): s.extend(raw_input().split()) s.sort() s=s[0:n] s=[int(x) for x in s] r[0]=s.count(0) for i in range(1,100): r[i]=r[i-1]+s.count(i) print " ".join(map(str,r))
相关文章推荐
- 【HackerRank】 The Full Counting Sort
- HackerRank - "Candles Counting" - DIFFICULT
- Hackerrank && Insertion Sort - Part 1
- hackerrank>Dashboard>C++>STL>Vector-Sort
- [hackerrank]Counting on a tree
- 【hackerrank】Insertion Sort Advanced Analysis
- [dfs序 主席树] HackerRank University CodeSprint 3. Simple Tree Counting
- [HackerRank University CodeSprint]Counting On a Tree
- 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)
- HackerRank - Insertion Sort Advanced Analysis
- HackerRank "Lena Sort"
- 【hackerrank】Week of Code 26
- [hackerrank Random]
- Hackerrank XOR Subsequences
- Hackerrank——Week of Code 27
- HackerRank "Xor and Sum"
- Hackerrank How many substrings
- Hackerrank World CodeSprint 9 Box Operations
- HackerRank "Median Updates"
- Counting-Sort