create a monotonic increasing stack

PHOTO EMBED

Fri Apr 05 2024 07:13:56 GMT+0000 (Coordinated Universal Time)

Saved by @hiimsa #java

// creating a monotonic stack
        int[] arr = new int[]{1,2,6,3,5,9, 11,7,};
        Deque<Integer> stack = new LinkedList<>();
        for(int i=0; i<arr.length; i++) {
            while(!stack.isEmpty() && stack.peek()< arr[i]) {
                stack.pop();
            }
            stack.push(arr[i]);
        }
        System.out.println("stack is " + stack);
content_copyCOPY