Simple Java solution


  • 0
    L
       public int findPeakElement(int[] nums) {
            if(nums.length==0) {
                return Integer.MIN_VALUE;
            }
            
            Stack<Integer> stack = new Stack<>();
            int pi = 0;
            stack.push(0);
            for(int i=1;i<nums.length;i++) {
                int currIdx = stack.peek();
                if(nums[i]>=nums[currIdx]) {
                    stack.pop();
                    pi = i;
                    stack.push(i);
                    continue;
                }
                break;
            }
            return stack.peek();
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.