Suspiciously easy? C#


  • 0
    D

    This passed as accepted and i was wondering what's the catch, as it seems too simple for a Medium difficulty task.

        public int FindPeakElement(int[] nums) {
            int length = nums.Length;
            if(length  == 0)
                return -1;
            if(length  == 1)
                return 0;
            if(nums[0] > nums[1])
                return 0;
            else if(length  == 2)
                return 1;
        
            for(int i = 1; i < length ; i++)
            {
                if (i + 1 == length )
                {
                    if (nums[i] > nums[i - 1])
                        return i;
                }
                if (nums[i - 1] < nums[i] && nums[i + 1] < nums[i])
                    return i;
            }
            return -1;
    }

  • 0
    G

    the catch is the solution should be O(lgn) complexity.


  • 0
    D

    Isn't my code O(n)?


  • 0
    S

    if O(n) is enough you can simply do the FindMax element and it should ok. Also the hidden task says the complexity should be O(log(n)) which most likely asks for binary search kind. But it conflicts with note that there might be more than 1 peak and you are doomed to miss the biggest one. Weird.


Log in to reply
 

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