Skip to content

Commit

Permalink
LeetCode901. Online Stock Span
Browse files Browse the repository at this point in the history
알고리즘: monotonic stack
복잡도: O(n), O(n)
사고과정
1. 어떻게 문제를 풀것인가? 그림을 그리며 시뮬레이션을 그리면 쉬워진다. stack에서 index를 유지하며 들어오는 값 '이하' 이면 pop한다. 그리고 최상단에 있는 인덱스와 지금 들어오는 인덱스의 차이를 반환한다.
  • Loading branch information
jinkshower committed Sep 26, 2024
1 parent fc7d9f5 commit be8cc11
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/review/leetcode901.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package review;

import java.util.ArrayDeque;
import java.util.Deque;

public class leetcode901 {
class StockSpanner {

Deque<int[]> stack;
int counter;

public StockSpanner() {
stack = new ArrayDeque<>();
counter = -1;
}

public int next(int price) {
counter++;
while (!stack.isEmpty() && stack.peek()[1] <= price) {
int[] top = stack.pop();
}
int tmp = -1;
if (!stack.isEmpty()) {
tmp = stack.peek()[0];
}

stack.push(new int[]{counter, price});
return counter - tmp;
}
}
}

0 comments on commit be8cc11

Please sign in to comment.