Advertisement
rajeshinternshala

Untitled

Oct 27th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1.     int solve(int prices[]) {
  2.         Stack<Integer> stack = new Stack<>();
  3.         int count = prices.length;
  4.         stack.add(0);
  5.         for (int i = 1; i < prices.length; i++) {
  6.  
  7.             while (!stack.isEmpty() && prices[stack.peek()] < prices[i]) {
  8.                 stack.pop();
  9.             }
  10.  
  11.             if (stack.isEmpty()) {
  12.                 count += i;
  13.                 stack.push(i);
  14.             } else {
  15.                 count += i - stack.peek() - 1;
  16.                 count += stack.size();
  17.                 stack.push(i);
  18.             }
  19.  
  20.         }
  21.         return count;
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement