[LeetCode] Find Minimum in Rotated Sorted Array

简介: As explained in the Solution tag, the key to solving this problem is to use invariants. We set two pointers: l for the left and r for the right.

As explained in the Solution tag, the key to solving this problem is to use invariants. We set two pointers: l for the left and r for the right. One key invariant is nums[l] > nums[r]. If this invariant does not hold, then we know the array has not been rotated and the minimum is justnums[l]. Otherwise, we reduce the search range by a half each time via comparisons betweennums[l], nums[r] and nums[(l + r) / 2], denoted as nums[mid]:

  1. If nums[mid] > nums[r], then mid is in the first larger half and r is in the second smaller half. So the minimum will be right to mid, update l = mid + 1;
  2. If nums[mid] <= nums[r], then the minimum is at least nums[mid] and elements right tomid cannot be the minimum. So update r = mid (note that it is not mid - 1 since midmay also be the index of the minimum).

When l == r or the invariant does not hold, we have found the answer, which is just nums[l].

Putting these togerther, we have the following codes.


C (0 ms)

1 int findMin(int* nums, int numsSize) {
2     int l = 0, r = numsSize - 1;
3     while (l < r && nums[l] > nums[r]) {
4         int mid = (l & r) + ((l ^ r) >> 1);
5         if (nums[mid] > nums[r]) l = mid + 1;
6         else r = mid; 
7     }
8     return nums[l];
9 }

C++ (4 ms)

 1 class Solution {
 2 public:
 3     int findMin(vector<int>& nums) {
 4         int l = 0, r = nums.size() - 1;
 5         while (l < r && nums[l] > nums[r]) {
 6             int mid = (l & r) + ((l ^ r) >> 1); 
 7             if (nums[mid] > nums[r]) l = mid + 1;
 8             else r = mid;
 9         }
10         return nums[l];
11     }
12 };

Python (48 ms)

 1 class Solution:
 2     # @param {integer[]} nums
 3     # @return {integer}
 4     def findMin(self, nums):
 5         l, r = 0, len(nums) - 1
 6         while l < r and nums[l] > nums[r]:
 7             mid = (l & r) + ((l ^ r) >> 1)
 8             if nums[mid] > nums[r]:
 9                 l = mid + 1
10             else:
11                 r = mid
12         return nums[l]

 

目录
相关文章
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
64 0
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
50 0
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
68 0
|
机器学习/深度学习 人工智能 算法
CF1550A Find The Array(贪心算法)
CF1550A Find The Array(贪心算法)
44 0
|
JavaScript 前端开发 索引
Array类型【find】
Array类型【find】
106 0
|
Shell Linux 索引
Linux- 转换 find 命令的返回值为 shell array
本文示例了如何在Linux系统下转换 find 命令的返回值为一个 shell array
256 0
|
5月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
6月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
76 6
|
6月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
146 2
|
3月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
99 1