-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStock.java
More file actions
39 lines (36 loc) · 1.04 KB
/
Stock.java
File metadata and controls
39 lines (36 loc) · 1.04 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
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Queue;
class Stock {
public int[] stock(int[] prices) {
Queue<Integer> q = new LinkedList<>();
ArrayList<Integer> downSeconds = new ArrayList<>();
int second = 0;
int index = 0;
for (int price : prices) {
q.offer(price);
}
while (!q.isEmpty()) {
index++;
if (index == prices.length) {
break;
}
for (int i = index; i < prices.length; i++) {
second++;
if (q.peek() > prices[i]) {
downSeconds.add(second);
second = 0;
q.poll();
break;
}
if (i == prices.length - 1) {
downSeconds.add(second);
second = 0;
q.poll();
}
}
}
downSeconds.add(0);
return downSeconds.stream().mapToint(i -> i, toArray());
}
}