一堆log有开始和结束点,每个log相当于一个进程。
design一个东西可以支持输入一个时间点,返回这个时间点有多少个进程在跑。
跟leetcode 732 My Calendar III 一个意思。
class Concurrent {
private TreeMap<Integer, Integer> times = new TreeMap<>();
public void book(int s, int e) {
times.put(s, times.getOrDefault(s, 0) + 1);
times.put(e, times.getOrDefault(e, 0) - 1);
}
public int getConcurrent(int s){
int res = 0;
for(int key : times.keySet()){
if(key <= s){
res += times.get(key);
}
}
return res;
}
public static void main(String[] args) {
Concurrent my = new Concurrent();
my.book(1,2);
my.book(1,3);
my.book(2,4);
int res = my.getConcurrent(3);
System.out.println(res);
}
}