Skip to content

Commit 1c4538c

Browse files
authored
Improved task 2034.
1 parent 873dde6 commit 1c4538c

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

src/main/java/g2001_2100/s2034_stock_price_fluctuation/StockPrice.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,24 @@
99

1010
public class StockPrice {
1111
private static class Record {
12-
Integer time;
13-
Integer price;
12+
int time;
13+
int price;
1414

15-
Record(Integer time, Integer price) {
15+
Record(int time, int price) {
1616
this.time = time;
1717
this.price = price;
1818
}
1919
}
2020

21-
Map<Integer, Integer> map;
22-
PriorityQueue<Record> maxHeap;
23-
PriorityQueue<Record> minHeap;
24-
Integer latestTimestamp = 0;
21+
private Map<Integer, Integer> map;
22+
private PriorityQueue<Record> maxHeap;
23+
private PriorityQueue<Record> minHeap;
24+
private int latestTimestamp = 0;
2525

2626
public StockPrice() {
2727
map = new HashMap<>();
28-
maxHeap =
29-
new PriorityQueue<>(
30-
(r1, r2) -> {
31-
if (r1.price.equals(r2.price)) {
32-
return 0;
33-
} else {
34-
return r1.price > r2.price ? -1 : 1;
35-
}
36-
});
37-
minHeap =
38-
new PriorityQueue<>(
39-
(r1, r2) -> {
40-
if (r1.price.equals(r2.price)) {
41-
return 0;
42-
} else {
43-
return r1.price < r2.price ? -1 : 1;
44-
}
45-
});
28+
maxHeap = new PriorityQueue<>((r1, r2) -> Integer.compare(r2.price, r1.price));
29+
minHeap = new PriorityQueue<>((r1, r2) -> Integer.compare(r1.price, r2.price));
4630
}
4731

4832
public void update(int timestamp, int price) {
@@ -58,19 +42,19 @@ public int current() {
5842

5943
public int maximum() {
6044
while (true) {
61-
Record r = maxHeap.peek();
62-
if (map.get(r.time).equals(r.price)) {
63-
return r.price;
45+
Record rec = maxHeap.peek();
46+
if (map.get(rec.time).equals(rec.price)) {
47+
return rec.price;
6448
}
6549
maxHeap.poll();
6650
}
6751
}
6852

6953
public int minimum() {
7054
while (true) {
71-
Record r = minHeap.peek();
72-
if (map.get(r.time).equals(r.price)) {
73-
return r.price;
55+
Record rec = minHeap.peek();
56+
if (map.get(rec.time).equals(rec.price)) {
57+
return rec.price;
7458
}
7559
minHeap.poll();
7660
}

0 commit comments

Comments
 (0)