【Python3】 LeetCode 7月挑战题目之25 - Find Minimum in Rotated Sorted Array II

第二十五天问:Find Minimum in Rotated Sorted Array II

这次题目是之前那道 Find Minimum in Rotated Sorted Array 的拓展:给出一个列表,列表中的数字是可能有多于一次重复,而且排列从小到大的但可能分段前后调转。然后,我们需要从这列表中,找出其中最小的一个。

大家好,我是一个喜欢研究算法、机械学习和生物计算的小青年,我的CSDN博客是:一骑代码走天涯
如果您喜欢我的笔记,那么请点一下关注、点赞和收藏。如果內容有錯或者有改进的空间,也可以在评论让我知道。😄

题目&示例 (引用自 LeetCode)

按此进入题目链结

Suppose an array sorted in ascending order 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.

Example 1:

Input: [1,3,5]
Output: 1

Example 2:

Input: [2,2,2,0,1]
Output: 0

Notes:

  • This is a follow up problem to Find Minimum in Rotated Sorted Array.
  • Would allow duplicates affect the run-time complexity? How and why?

解题思路

因为数字具有重复性,所以无法利用二分查找法的机制,也就是无法取得 O( log ⁡ n \log n logn) 的时间复杂度。比较简单的方法,就是一个个的搜,直到找到最小的那个数字,时间复杂度为_O_( ​ n ​n n)。

代码

时间复杂度:O( ​ n ​n n)
空间复杂度:O( 1 1 1)

class Solution:
    def findMin(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return nums[0]
        start = 0
        res = nums[0]
        while start < len(nums):
            if nums[start] <= res:
                res = nums[start]
            start += 1
        return res
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值