Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

本文介绍了一种在O(logn)时间内找到旋转排序数组中的最小值的方法。通过二分查找的方式,利用数组旋转前后两个有序递增部分的特点,逐步缩小搜索范围,最终定位到最小值的位置。

O(n)的算法就不说了,这题主要考查的是 O(logn)的算法。


有序数组容易想到使用二分查找解决,这题就是在二分基础上做一些调整。数组只有一次翻转,可以知道原有序递增数组被分成两部分,这俩部分都是有序递增的(这题只需要考虑有序数组的递增情况)。


假如翻转后的数组以第 x 个结点分为两部分 A[0..x] 和 A[x+1..n]。则 A[0..x] 这一段是有序递增的, A[x+1..n] 这一段也是有序递增的。并且因为原数组是有序递增的,A[0..x] 中所有数都会大于 A[x+1..n] 中的任何数。所以我们其实就是需要找到结点 A[x+1],这个结点的值就是最小值。


考虑数组 A[i..j],中间结点 m (m = (i + j ) / 2)。


A[i] < A[j]:数组是递增的,说明已经找到 x 结点,并且 x 等于 i。


A[i] >= A[j]:数组不是递增的,说明 x 结点还没有找到,这时对比中间结点 A[m]


A[m] > A[i]: 则数组中 A[i..m] 这一段是有序递增的,翻转结点 x 定不会在这一段中,这时我们只需要考虑 A[m+1..j] 这一段。


A[m] < A[i]:说明翻转结点 x 在 A[i..m]中。


另外特别考虑只有一个元素的情况。



public class Solution {
    public int findMin(int[] num) {
        
        int left = 0;
        int right = num.length - 1;
        
        while(left < right)
        {
            if(num[left] < num[right]) {
                return num[left];
            }
            
            int mid = left + (right-left)/2;
            if(num[left] <= num[mid]) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return num[left];
    }
}

http://orzorz.me/learn/lesson.htm?lessonId=106

递归

Thoughts:

  1. If the array just has one element, then return the element.
  2. If the array has two elements, then return the smaller one.
  3. If the left most element is smaller than the right most element, then we can know the array is sorted like never be rotated. Just return the left one.
  4. By the method of Binary Search, we get the middle element of array, a[mid]. If a[mid] > a[left], then the left half of array is sorted. we then search the right half, including a[mid]. Otherwise we search the left half, including a[mid].

public class Solution {
    public int findMin(int[] A) {
        return helper(A, 0, A.length-1);
    }
    
    public static int helper(int[] a, int left, int right){
        //one element
        if(left == right){
            return a[left];
        }
         
        //two elements
        if(left == right-1){
            return a[left]<a[right]? a[left]: a[right];
        }
         
        //the array is ordered
        if(a[left] < a[right]){
            return a[left];
        }
         
        int mid = (left+right)/2;
         
        if(a[mid] >= a[left]){
            return helper(a, mid, right);
        }else{
            return helper(a, left, mid);
        }
         
    }
}



https://chesterli0130.wordpress.com/2012/10/20/finding-the-minimum-in-a-sorted-rotated-array/



本地跟单专家顾问(EA)是一种专为MetaTrader 4平台设计的自动化交易工具。该版本强调其无限制特性,允许用户在任何时段、不同地理区域及各类账户上自由部署,从而为交易者提供了高度灵活的操作空间。其核心机制采用同向复制策略,即接收端会完全模仿发送端的交易方向与操作,适合那些信赖信号源稳定性的用户,以期通过跟随策略实现相近的投资回报。 系统架构包含两个独立模块:信号发送端与信号接收端。发送端安装于主导交易决策的账户,接收端则配置于需同步执行的账户,二者协同工作,实现了交易指令的自动传递与执行,有效减少了人工干预的需求。此外,该工具特别注重与MT4服务器时间的同步,确保交易执行时点的精确性,避免因时区偏差可能引发的操作失误,这对于依赖时间敏感性的外汇市场尤为重要。 文件标识中的特定代号可能指向开发者的内部版本标记或某种定制化交易逻辑,具体含义需结合进一步的技术文档予以确认。整体而言,该EA为多账户管理与策略复制提供了一个集成化解决方案,有助于提升交易执行的效率并降低操作风险。但需注意,市场环境处于持续变动中,任何自动化工具均需经过充分验证与适应性测试,历史表现不能作为未来收益的保证。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值