-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockSpanner.java
More file actions
114 lines (83 loc) · 2.43 KB
/
Copy pathStockSpanner.java
File metadata and controls
114 lines (83 loc) · 2.43 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.rbhatt.stack;
import java.util.Stack;
//design
//stack
//Leetcode question # 901
public class StockSpanner {
public class StockSpan {
private int price;
private int span;
StockSpan(int price, int span) {
this.price = price;
this.span = span;
}
}
Stack<StockSpan> stack;
/*List<Integer> stockPrice;
int previousDayPrice;
int maxPrice;*/
public StockSpanner() {
stack = new Stack<>();
/*stockPrice = new ArrayList<>();
previousDayPrice = 0;
maxPrice = Integer.MIN_VALUE;*/
}
public int next(int price) {
/*if (stack.size() == 0) {
stack.push(new StockSpan(price, 1));
return 1;
}*/
int span = 1;
while(!stack.empty() && price >= stack.peek().price) {
span += stack.pop().span;
}
stack.push(new StockSpan(price, span));
return span;
}
/*public int next(int price) {
if (stockPrice.size() == 0) {
stockPrice.add(price);
maxPrice = price;
previousDayPrice = price;
return 1;
}
if (price < previousDayPrice) {
previousDayPrice = price;
stockPrice.add(price);
return 1;
}
if (price > maxPrice) {
previousDayPrice = price;
maxPrice = price;
stockPrice.add(price);
return stockPrice.size();
}
int span = 1;
for (int i = stockPrice.size() - 1; i >= 0; i--) {
if (price < stockPrice.get(i)) {
break;
}
span++;
}
previousDayPrice = price;
stockPrice.add(price);
return span;
}*/
public static void main(String[] args) {
StockSpanner ob = new StockSpanner();
System.out.println(ob.next(100));
System.out.println(ob.next(80));
System.out.println(ob.next(60));
System.out.println(ob.next(70));
System.out.println(ob.next(60));
System.out.println(ob.next(75));
System.out.println(ob.next(85));
System.out.println();
StockSpanner ob1 = new StockSpanner();
System.out.println(ob1.next(29));
System.out.println(ob1.next(91));
System.out.println(ob1.next(62));
System.out.println(ob1.next(76));
System.out.println(ob1.next(51));
}
}