-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuru_Interview.java
More file actions
63 lines (50 loc) · 1.79 KB
/
Guru_Interview.java
File metadata and controls
63 lines (50 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.HashMap;
import java.util.Map;
public class Guru_Interview {
public static void main(String[] args) {
System.out.println(solution("00:01:07,400-234-090\n" +
"00:05:01,701-080-080\n" +
"00:05:00,400-234-090"));
}
public static int solution(String S) {
//Split the strings
String[] lines = S.split("\n");
Map<String,Integer> numToTime = new HashMap<String, Integer>();
Map<String,Double> numToPay = new HashMap<String, Double>();
int totalPay=0;
for(String line:lines) {
String duration = line.split(",")[0];
String number = line.split(",")[1];
int hours = Integer.parseInt(duration.split(":")[0]);
int mins = Integer.parseInt(duration.split(":")[1]);
int secs = Integer.parseInt(duration.split(":")[2]);
int totalSecs = hours*3600 + mins*60 + secs;
double payAmount = 0;
//Calculate the amount for each entry
if(totalSecs < 300) {
payAmount = totalSecs * 3;
}else{
payAmount = Math.ceil((double)totalSecs/60) * 150;
}
totalPay = (int) (totalPay + payAmount);
if(!numToTime.containsKey(number)) {
numToTime.put(number, totalSecs);
numToPay.put(number, payAmount);
}else {
numToTime.put(number, numToTime.get(number) + totalSecs);
numToPay.put(number, numToPay.get(number) + payAmount);
}
}
//Get the free call
Map.Entry<String, Integer> maxEntry = null;
for(Map.Entry<String, Integer> entry:numToTime.entrySet()){
if(maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
maxEntry = entry;
}else if(entry.getValue().compareTo(maxEntry.getValue()) == 0) {
maxEntry = maxEntry;
}
}
totalPay = (int) (totalPay - numToPay.get(maxEntry.getKey()));
return totalPay;
}
}