Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int solve(int prices[]) {
- Stack<Integer> stack = new Stack<>();
- int count = prices.length;
- stack.add(0);
- for (int i = 1; i < prices.length; i++) {
- while (!stack.isEmpty() && prices[stack.peek()] < prices[i]) {
- stack.pop();
- }
- if (stack.isEmpty()) {
- count += i;
- stack.push(i);
- } else {
- count += i - stack.peek() - 1;
- count += stack.size();
- stack.push(i);
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement