Skip to content

Commit 1dc966d

Browse files
committed
Leetcode | Online Stock Span | Java
1 parent e3b909e commit 1dc966d

3 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 901. Online Stock Span
2+
3+
Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the
4+
current day.
5+
6+
The span of the stock's price in one day is the maximum number of consecutive days (starting from that day and going
7+
backward) for which the stock price was less than or equal to the price of that day.
8+
9+
- For example, if the prices of the stock in the last four days is `[7,2,1,2]` and the price of the stock today is `2`,
10+
then the span of today is `4` because starting from today, the price of the stock was less than or equal `2` for `4`
11+
consecutive days.
12+
- Also, if the prices of the stock in the last four days is `[7,34,1,2]` and the price of the stock today is `8`, then
13+
the span of today is `3` because starting from today, the price of the stock was less than or equal `8` for `3`
14+
consecutive days.
15+
16+
Implement the StockSpanner class:
17+
18+
- `StockSpanner()` Initializes the object of the class.
19+
- `int next(int price)` Returns the span of the stock's price given that today's price is `price`.
20+
21+
Example 1:
22+
23+
Input
24+
["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
25+
[[], [100], [80], [60], [70], [60], [75], [85]]
26+
Output
27+
[null, 1, 1, 1, 2, 1, 4, 6]
28+
29+
Explanation
30+
StockSpanner stockSpanner = new StockSpanner();
31+
stockSpanner.next(100); // return 1
32+
stockSpanner.next(80); // return 1
33+
stockSpanner.next(60); // return 1
34+
stockSpanner.next(70); // return 2
35+
stockSpanner.next(60); // return 1
36+
stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to
37+
today's price.
38+
stockSpanner.next(85); // return 6
39+
40+
Constraints:
41+
42+
- `1 <= price <= 10^5`
43+
- At most `10^4` calls will be made to `next`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.devstromo.medium.p901;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class StockSpanner {
7+
private final List<Integer> list;
8+
9+
public StockSpanner() {
10+
this.list = new ArrayList<>();
11+
}
12+
13+
public int next(int price) {
14+
list.add(price);
15+
int count = 0;
16+
for (int i = list.size() - 1; i >= 0; i--) {
17+
if (list.get(i) <= price) {
18+
count++;
19+
} else {
20+
break;
21+
}
22+
}
23+
return count;
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.devstromo.medium.p901;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class StockSpannerUnitTest {
9+
10+
@Test
11+
@DisplayName("Test Online Stock Span")
12+
void testOnlineStockSpan() {
13+
StockSpanner stockSpanner = new StockSpanner();
14+
int result = 0;
15+
result = stockSpanner.next(100); // return 1
16+
assertEquals(1, result);
17+
result = stockSpanner.next(80); // return 1
18+
assertEquals(1, result);
19+
result = stockSpanner.next(60); // return 1
20+
assertEquals(1, result);
21+
result = stockSpanner.next(70); // return 2
22+
assertEquals(2, result);
23+
result = stockSpanner.next(60); // return 1
24+
assertEquals(1, result);
25+
result = stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.
26+
assertEquals(4, result);
27+
result = stockSpanner.next(85); // return 6
28+
assertEquals(6, result);
29+
}
30+
}

0 commit comments

Comments
 (0)