-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockPrice.java
More file actions
53 lines (45 loc) · 1.76 KB
/
Copy pathStockPrice.java
File metadata and controls
53 lines (45 loc) · 1.76 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
package leetcode;
import java.util.*;
// 股票波动 https://leetcode-cn.com/problems/stock-price-fluctuation/solution/gu-piao-jie-ge-bo-dong-by-leetcode-solut-rwrb/
// 优先队列维护最小最大,hashmap记录所有记录,优先队列出队时如果和hashmap中不一致代表已经被更新
public class StockPrice {
int maxTimestamp;
HashMap<Integer, Integer> timePriceMap;
PriorityQueue<int[]> pqMax;
PriorityQueue<int[]> pqMin;
public StockPrice() {
maxTimestamp = 0;
timePriceMap = new HashMap<Integer, Integer>();
pqMax = new PriorityQueue<int[]>((a, b) -> b[0] - a[0]);
pqMin = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);
}
public void update(int timestamp, int price) {
maxTimestamp = Math.max(maxTimestamp, timestamp);
timePriceMap.put(timestamp, price);
pqMax.offer(new int[]{price, timestamp});
pqMin.offer(new int[]{price, timestamp});
}
public int current() {
return timePriceMap.get(maxTimestamp);
}
public int maximum() {
while (true) {
int[] priceTime = pqMax.peek();
int price = priceTime[0], timestamp = priceTime[1];
if (timePriceMap.get(timestamp) == price) {
return price;
}
pqMax.poll();
}
}
public int minimum() {
while (true) {
int[] priceTime = pqMin.peek();
int price = priceTime[0], timestamp = priceTime[1];
if (timePriceMap.get(timestamp) == price) {
return price;
}
pqMin.poll();
}
}
}