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

Commit 4ae6026

Browse files
add java solution of 901
1 parent f5e7105 commit 4ae6026

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

‎README.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,17 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
792792
|898|[Bitwise ORs of Subarrays](https://leetcode.com/problems/bitwise-ors-of-subarrays/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_898.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
793793
|899|[Orderly Queue](https://leetcode.com/problems/orderly-queue/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_899.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
794794
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_900.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
795-
795+
|901|[Online Stock Span](https://leetcode.com/problems/online-stock-span/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_901.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
796+
|902|[Numbers At Most N Given Digit Set]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_902.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
797+
|903|[Valid Permutations for DI Sequence]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_903.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
798+
|904|[Fruit Into Baskets]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_904.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
799+
|905|[Sort Array By Parity]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_905.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
800+
|906|[Super Palindromes]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_906.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
801+
|907|[Sum of Subarray Minimums]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_907.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
802+
|908|[Smallest Range I]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_908.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
803+
|909|[Snakes and Ladders]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_909.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
804+
|910|[Smallest Range II]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_910.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
805+
|911|[Online Election]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_911.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
796806

797807

798808

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.hit.basmath.learn.others;
2+
3+
/**
4+
* 901. Online Stock Span
5+
* <p>
6+
* Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day.
7+
* <p>
8+
* The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.
9+
* <p>
10+
* For example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].
11+
* <p>
12+
* Example 1:
13+
* <p>
14+
* Input: ["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]]
15+
* Output: [null,1,1,1,2,1,4,6]
16+
* Explanation:
17+
* First, S = StockSpanner() is initialized. Then:
18+
* S.next(100) is called and returns 1,
19+
* S.next(80) is called and returns 1,
20+
* S.next(60) is called and returns 1,
21+
* S.next(70) is called and returns 2,
22+
* S.next(60) is called and returns 1,
23+
* S.next(75) is called and returns 4,
24+
* S.next(85) is called and returns 6.
25+
* <p>
26+
* Note that (for example) S.next(75) returned 4, because the last 4 prices
27+
* (including today's price of 75) were less than or equal to today's price.
28+
* <p>
29+
* Note:
30+
* <p>
31+
* Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5.
32+
* There will be at most 10000 calls to StockSpanner.next per test case.
33+
* There will be at most 150000 calls to StockSpanner.next across all test cases.
34+
* The total time limit for this problem has been reduced by 75% for C++, and 50% for all other languages.
35+
*/
36+
public class _901 {
37+
class StockSpanner {
38+
int size;
39+
int[] arr;
40+
int[] map;
41+
42+
public StockSpanner() {
43+
arr = new int[10000];
44+
map = new int[10000];
45+
size = 0;
46+
}
47+
48+
public int next(int price) {
49+
arr[size++] = price;
50+
int ret = 1;
51+
if (size == 1) {
52+
map[size - 1] = 1;
53+
return 1;
54+
}
55+
int tmp = size - 1;
56+
while (tmp > 0 && arr[tmp - 1] <= price) {
57+
ret += map[tmp - 1];
58+
tmp = tmp - map[tmp - 1];
59+
}
60+
map[size - 1] = ret;
61+
return ret;
62+
}
63+
}
64+
}

0 commit comments

Comments
(0)

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