smj007

Untitled

Jun 21st, 2022
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. class Solution:
  2.    
  3.     def f(self, nums, mid, left, right):
  4.        
  5.         # first element which satisfied the below condition
  6.         # is the minimum element of the array
  7.         if (nums[mid] < nums[right]):
  8.             return True
  9.         else:
  10.             return False
  11.        
  12.        
  13.     def findMin(self, nums: List[int]) -> int:
  14.        
  15.         left = 0
  16.         right = len(nums) - 1
  17.        
  18.         # template for finding first inflection point or pivot point.
  19.         # This is useful for finding first element which satisfies a condition
  20.         while(left < right):
  21.            
  22.             mid = left + (right - left)//2
  23.            
  24.             if self.f(nums, mid, left, right):
  25.                 right = mid
  26.             else:
  27.                 left = mid + 1
  28.        
  29.         # base case when array is not rotated
  30.         return nums[left]
Advertisement
Add Comment
Please, Sign In to add comment