Skip to content

Commit 7606fbd

Browse files
add 2034
1 parent 34703ec commit 7606fbd

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2034|[Stock Price Fluctuation](https://leetcode.com/problems/stock-price-fluctuation/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2034.java) ||Medium||
1112
|2033|[Minimum Operations to Make a Uni-Value Grid](https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2033.java) ||Medium||
1213
|2032|[Two Out of Three](https://leetcode.com/problems/two-out-of-three/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2032.java) ||Easy||
1314
|2028|[Find Missing Observations](https://leetcode.com/problems/find-missing-observations/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2028.java) ||Medium||
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.TreeSet;
6+
7+
public class _2034 {
8+
public static class Solution1 {
9+
class StockPrice {
10+
TreeSet<Integer> treeSet;
11+
Map<Integer, Integer> map;
12+
Map<Integer, Integer> countMap;
13+
int current;
14+
15+
public StockPrice() {
16+
treeSet = new TreeSet<>();
17+
map = new HashMap<>();
18+
countMap = new HashMap<>();
19+
current = 0;
20+
}
21+
22+
public void update(int timestamp, int price) {
23+
if (map.containsKey(timestamp)) {
24+
int previousPrice = map.get(timestamp);
25+
countMap.put(previousPrice, countMap.getOrDefault(previousPrice, 0) - 1);
26+
if (countMap.get(previousPrice) <= 0) {
27+
countMap.remove(previousPrice);
28+
treeSet.remove(previousPrice);
29+
}
30+
}
31+
map.put(timestamp, price);
32+
treeSet.add(price);
33+
countMap.put(price, countMap.getOrDefault(price, 0) + 1);
34+
if (current < timestamp) {
35+
current = timestamp;
36+
}
37+
}
38+
39+
public int current() {
40+
return map.get(current);
41+
}
42+
43+
public int maximum() {
44+
return treeSet.last();
45+
}
46+
47+
public int minimum() {
48+
return treeSet.first();
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)