简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

题目:

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.

这是 LeetCode 上的一道算法题,题意是一个已经排序的数组,截断后重新拼接,找出数组中最小的数字。
这道题目用 Python 来实现的话太简单了。代码如下:

class Solution:
    # @param num, a list of integer
    # @return an integer
    def findMin(self, num):
        return min(num)

用了 Python 内置的 min 函数,提交后通过,耗时是:Runtime: 184 ms
完成后被吐槽:“你这样有意思么?”,额,的确很没意思。那就考虑优化一下吧。
Python 内置的 min 函数是会遍历整个数组的,时间复杂度为 O(n),不过这个数组本来是有序的了,所以可以稍微做点优化,代码如下:

class Solution:
    # @param num, a list of integer
    # @return an integer
    def findMin(self, num):
        for i in range(0, len(num)-1):
            if(num[i] > num[i+1]):
                return num[i+1]
        return num[0]

当找到第一个值小于它之前的数字的时候,就可以结束循环了。此时代码理论上的复杂度还是 O(n),但实际上是会快一点点的。
提交后通过,看测试结果,耗时是:Runtime: 164 ms,看来优化还是有点效果的,快了一点点。

还能不能在快一点呢?题目中提到的 Sorted (已排序),很容易让人想到使用二分法来查找,不过这个排序不是真的排序,是被截断过的,所以要稍微做些变通。尝试加入二分查找的代码如下:

class Solution:
    # @param num, a list of integer
    # @return an integer
    def findMin(self, num):
        i = 0
        j = len(num)-1
        while(i < j-1):
            point = int((i + j)/2)
            if(num[point] > num[i]):
                i = point
            if(num[point] < num[j]):
                j = point
        return min(num[0], num[i], num[j])

使用二分法不断逼近最小的数字,此时代码的时间复杂度为 O(log2n) ,提交后通过,看测试结果,耗时是:Runtime: 140 ms。效果还行。
这个结果应该是我能做到的最优结果了。

时间: 2024-11-16 06:05:31

简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。的相关文章

LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unknown to you be

leetcode 【 Find Minimum in Rotated Sorted Array 】python 实现

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 代码:oj测试通过 Runtime: 52 ms 1 class Solution: 2 #

[leetcode]Find Minimum in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中可能有相同的数.那么,分下列几种情况: 代码: class Solution: # @param num, a list of integer # @return an integer def findMin(self, num): L = 0; R = len(num)-1 while L < R

【LeetCode-面试算法经典-Java实现】【153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)】

[153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You m

【LeetCode-面试算法经典-Java实现】【155-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】

[154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Supp

Hard??没搞错吧,LeetCode水题一道:Find Minimum in Rotated Sorted Array II

做完Find Minimum in Rotated Sorted Array I觉得很简单,再做2发现也不难,但是LeetCode竟然给了一个hard评级,难道是我编程水平大有长进?哈哈我估计是对复杂度有很高的要求的情况下比较难吧.. Python(偷懒)做法就是一句话:return min(num) Java(基本做法)如下: public class Solution { public int findMin(int[] num) { int len=num.length; if (len=

[LeetCode]111. Find Minimum in Rotated Sorted Array II旋转数组最小值II

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might be

Find Minimum in Rotated Sorted Array II -- LeetCode

这道题是Search in Rotated Sorted Array的扩展,思路在Find Minimum in Rotated Sorted Array中已经介绍过了,和Find Minimum in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现.不过正是因为这个条件的出现,影响到了算法的时间复杂度.原来我们是依靠中间和边缘元素的大小关系,来判断哪一半是不受rotate影响,仍然有序的.而现在因为重复的出现,如果我们遇到中间和边缘相等的情况,我们就无法判

[C++]LeetCode: 132 Find Minimum in Rotated Sorted Array II (二分查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. 思路:这道题是Search in Rotated Sorted Array的扩展.和Find Minimum in Rot