The Method that used
Map<String, Map<String,Integer>>
Problem description
Huawei Mall is running a promotion where the earliest orders (or multiple orders) in a given second are free.
Now, given a batch of order records, calculate how many orders can be waived.
Samples
Input:
5
2019-01-01 00:00:00.004
2019-01-01 00:00:00.004
2019-01-01 00:00:01.006
2019-01-01 00:00:01.006
2019-01-01 00:00:01.005
Output:
3
My Answer
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Main {
private static int freeOrder(String[] orderTime) {
Map<String, Map<String,Integer>> map = new HashMap<>();
for(String str: orderTime){
String [] ss = str.split("\\.");
if (!map.containsKey(ss[0])) {
map.put(ss[0], new TreeMap<>());
}
if(!map.get(ss[0]).containsKey(ss[1])){
map.get(ss[0]).put(ss[1],1);
}
else{
int temp = map.get(ss[0]).get(ss[1]);
map.get(ss[0]).put(ss[1],1+temp);
}
}
int s=0;
for(Map.Entry<String, Map<String, Integer>> entry:map.entrySet()){
String ke = entry.getKey();
Map<String, Integer> va = entry.getValue();
for(Map.Entry<String, Integer> en:va.entrySet()){
s+=en.getValue();
break;
}
}
return s;
}
}