A neat O(N) solution


  • -1
    R
     public class Solution {
        public int findPeakElement(int[] num) {
            
            if(num == null)
                return 0;
            int len = num.length;
            if(len == 0)
                return 0;
            int i = 0;
            while(++i < len){
                if(num[i] < num[i - 1])
                    return (i - 1);
            }
            return (len - 1);
        }
    }
    

    just find the element which is less than its previous one.


  • 1
    S

    O(n) solution is trivial and is not an acceptable answer to this interview question. Please try to come up with an O(logn) algorithm.


Log in to reply
 

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