Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit c2e136c

Browse files
added problem75
1 parent d1852d2 commit c2e136c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎problem75/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 current day.
4+
5+
The span of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day.
6+
7+
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, then the span of today is 4 because starting from today, the price of the stock was less than or equal 2 for 4 consecutive days.
8+
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 the span of today is 3 because starting from today, the price of the stock was less than or equal 8 for 3 consecutive days.
9+
Implement the StockSpanner class:
10+
11+
StockSpanner() Initializes the object of the class.
12+
int next(int price) Returns the span of the stock's price given that today's price is price.
13+
14+
15+
## Example 1:
16+
17+
Input
18+
["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
19+
[[], [100], [80], [60], [70], [60], [75], [85]]
20+
Output
21+
[null, 1, 1, 1, 2, 1, 4, 6]
22+
23+
Explanation
24+
StockSpanner stockSpanner = new StockSpanner();
25+
stockSpanner.next(100); // return 1
26+
stockSpanner.next(80); // return 1
27+
stockSpanner.next(60); // return 1
28+
stockSpanner.next(70); // return 2
29+
stockSpanner.next(60); // return 1
30+
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.
31+
stockSpanner.next(85); // return 6
32+
33+
34+
## Constraints:
35+
36+
1 <= price <= 105
37+
At most 104 calls will be made to next.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /