-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUber.java
More file actions
58 lines (48 loc) · 1.36 KB
/
Uber.java
File metadata and controls
58 lines (48 loc) · 1.36 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/*
Given a list of tasks and a cooldown to be respected for the same type of task, calculate how long it would take to execute the entire list of tasks
Ex 1:
tasks = [5, 5, 3, 5, 3]
cooldown = 2
execution = [5, _, _, 5, 3, _, 5, 3]
output = 8
*/
public class Uber {
public static void main(String args[]) throws Exception {
int[] tasks = { 5, 5, 3, 5, 3 };
int cd = 2;
System.out.println(new Uber().getRunTime(tasks, cd));
}
public int getRunTime(int[] tasks, int cd) {
if(tasks == null || tasks.length == 0){
return 0;
}
Map<Integer, Integer> map = new HashMap<>();
int i=0;
int time = 0;
while(i< tasks.length){
if(!map.containsKey(tasks[i]) ){
time++;
map.put(tasks[i], time);
i++;
} else {
int index = map.get(tasks[i]);
if(time - index >= cd){
time++;
map.put(tasks[i], time);
i++;
} else{
time = time + (cd - (time-index));
time++;
map.put(tasks[i], time);
i++;
}
}
}
return time;
}
}