Advertisement
RupeshAcharya60

Untitled

Aug 7th, 2024
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. def findMin(nums):
  2.     """
  3.    :type nums: List[int]
  4.    :rtype: int
  5.    """
  6.     N = len(nums)
  7.     hi= N-1
  8.     lo = 0
  9.    
  10.     if nums[lo]<nums[hi]:
  11.         return nums[lo]
  12.    
  13.     while lo<=hi:
  14.        
  15.         mid = (lo+hi)//2
  16.         next = (mid+1)%N
  17.         prev = (mid+N-1)%N
  18.  
  19.         if nums[mid]<=nums[next] and nums[mid]<=nums[prev]:
  20.             return nums[mid]
  21.         elif nums[0]<nums[mid]:
  22.             lo = mid + 1
  23.         elif nums[N-1]>nums[mid]:
  24.             hi = mid - 1
  25.            
  26.            
  27. print(findMin([11,13,15,17]))
  28.  
  29. output : 11
  30.  
  31.  
  32. class Solution(object):
  33.     def findMin(self, nums):
  34.         """
  35.        :type nums: List[int]
  36.        :rtype: int
  37.        """
  38.         N = len(nums)
  39.         hi= N-1
  40.         lo = 0
  41.  
  42.         while lo<=hi:
  43.             mid = (lo+hi)//2
  44.             nxt = (mid+1)%N
  45.             prev = (mid+N-1)%N
  46.             if nums[mid]<=nums[nxt] and nums[mid]<=nums[prev]:
  47.                 return nums[mid]
  48.             elif nums[0]<nums[mid]:
  49.                 lo = mid + 1
  50.             elif nums[N-1]>nums[mid]:
  51.                 hi = mid - 1
  52.  
  53. output : None
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement