C# solution focusing on edge cases


  • 0
    E
    public int FindMin(int[] nums) 
        {
            if(nums.Length == 0) return 0;
            if(nums.Length == 1) return nums[0];
            
            int leftIndex = 0;
            int rightIndex = nums.Length -1;
    
            while(nums[leftIndex] > nums[rightIndex])
            {
                leftIndex++;
                rightIndex--;
            }
            
            if(leftIndex == 0)
            {
               return Math.Min(nums[leftIndex], nums[rightIndex]);
            }
            
            if(nums[leftIndex] < nums[rightIndex])
            {
                return Math.Min(nums[rightIndex+1], nums[leftIndex]);
            }
            
            return Math.Min(nums[rightIndex], nums[rightIndex+1]);
        }

Log in to reply
 

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