forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2034.java
52 lines (45 loc) · 1.54 KB
/
_2034.java
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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
public class _2034 {
public static class Solution1 {
class StockPrice {
TreeSet<Integer> treeSet;
Map<Integer, Integer> map;
Map<Integer, Integer> countMap;
int current;
public StockPrice() {
treeSet = new TreeSet<>();
map = new HashMap<>();
countMap = new HashMap<>();
current = 0;
}
public void update(int timestamp, int price) {
if (map.containsKey(timestamp)) {
int previousPrice = map.get(timestamp);
countMap.put(previousPrice, countMap.getOrDefault(previousPrice, 0) - 1);
if (countMap.get(previousPrice) <= 0) {
countMap.remove(previousPrice);
treeSet.remove(previousPrice);
}
}
map.put(timestamp, price);
treeSet.add(price);
countMap.put(price, countMap.getOrDefault(price, 0) + 1);
if (current < timestamp) {
current = timestamp;
}
}
public int current() {
return map.get(current);
}
public int maximum() {
return treeSet.last();
}
public int minimum() {
return treeSet.first();
}
}
}
}