150强访谈简单

查找旋转排序数组中的最小值

“查找旋转排序数组中的最小值”问题的详细指南和 Python 实现。

问题陈述

简单

假设按升序排序的长度为 n 的数组在 1 到 n 次之间循环。例如,如果数组 [0,1,2,4,5,6,7] 旋转 4 次,则可能会变成 [4,5,6,7,0,1,2]

请注意,将数组旋转 [a[0], a[1], ..., a[n-1]] 1 次会得到 [a[n-1], a[0], a[1], ..., a[n-2]]

给定唯一元素的排序旋转数组 nums,返回该数组的最小元素。

您必须编写一个在 O(log n) 时间内运行的算法。

编写一个函数 findMin(nums: List[int]) -> int

约束条件
  • n == len(nums)
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • All integers in nums are unique
  • nums is sorted and rotated between 1 and n times

示例

Example 1
Input
nums = [3, 4, 5, 1, 2]
Output
1
Explanation

The original array was [1,2,3,4,5] rotated 3 times. The minimum is 1.

Example 2
Input
nums = [4, 5, 6, 7, 0, 1, 2]
Output
0
Explanation

The original array was [0,1,2,4,5,6,7] rotated 4 times. The minimum is 0.

Example 3
Input
nums = [11, 13, 15, 17]
Output
11
Explanation

Array is not rotated (or rotated n times). The minimum is the first element.

Need a Hint?
考虑使用二分搜索特定的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。