[
binary search
]
BinarySearch 0033 Find the Largest Number in a Rotated List
Problem statement
https://binarysearch.com/problems/Find-the-Largest-Number-in-a-Rotated-List/
Solution
Almost the same as Leetcode 153. Find Minimum in Rotated Sorted Array, but here we need to find maximum.
Complexity
It is O(log n)
for time and O(1)
for space.
Code
class Solution:
def solve(self, nums):
n = len(nums)
def dfs(start, end):
if end - start <= 1:
return max(nums[(start-1)%n], nums[start])
mid = (start + end)//2
if nums[end] > nums[mid]:
return dfs(start, mid)
else:
return dfs(mid + 1, end)
return dfs(0, len(nums) - 1)