已挂
3 Problems, 90 minutes.
Problem 1
Adding sequence number to the end of the repeating names.
Input
[“tom”,“alex”,“jack”,“Alex”]
Ouput
[“tom”,“alex”,'jack",“alex1”]
Explanation
Name “alex” apears twice, the first one can be ignored, sequence number should be added from the second alex.
Solution
Count as you go in a HashMap and ignore the first index 0.
Problem 2
没找到原题。HankerRank上的,题目叫Selling Products。
Take out the given number of elements in the list so that the uniqueness is low.
Input
ids = [1,2,3,1,2,2] m=3
Output
1
Explanation
We can remove 3, 1, 1 and leave only 2 from the input array. Where 1 represents the number of unique number left in the list. In this case, it is 1.
Solution
-
Create a class Item with property count and id. id here is the number from the input array.
-
Count all the unique elements via a HashMap, once it is done output them in a List. Sort it in desending order.
-
From the end, decrease the count while m–. When count is 0, remove it from the list.
-
Return the size of the list.
Problem 3
Looks like a problem similar as word sequence which requires dynamic programing knowledge.