Iam_Sandeep

Find minimum in rotated sorted array

Jul 2nd, 2022 (edited)
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. #use this only
  2. class Solution:
  3.     def findMin(self, a: List[int]) -> int:
  4.         n=len(a)
  5.         if len(a)==1:return a[0]
  6.         l,r=0,len(a)-1
  7.         if a[0]<a[-1]:return a[0]
  8.         while l<=r:
  9.             m=(l+r)>>1
  10.             if m-1>=0 and a[m-1]>a[m]:return a[m]
  11.             if a[m]>a[m+1]:return a[m+1]
  12.             if a[0]<a[m]:
  13.                 l=m+1
  14.             else:
  15.                 r=m-1
  16.    
  17. ------------------------------------------------------------------------------------------------------------------------
  18. #Always compare with right one
  19. #Better one
  20. class Solution:
  21.     def findMin(self, a: List[int]) -> int:
  22.         n=len(a)
  23.         l,r=0,n-1
  24.         while l<r:
  25.             m=(l+r)>>1
  26.             if a[m]>a[-1]:
  27.                 l=m+1
  28.             else:
  29.                 r=m
  30.         return a[l]
Advertisement
Add Comment
Please, Sign In to add comment