📜  数组中的最小值,该数组先减小然后增大

📅  最后修改于: 2021-04-22 09:31:37             🧑  作者: Mango

给定一个由N个整数组成的数组,其中数组元素形成严格减少和增加的序列。任务是在这样的数组中找到最小的数字。
限制条件:N> = 3
例子:

Input: a[] = {2, 1, 2, 3, 4}
Output: 1

Input: a[] = {8, 5, 4, 3, 4, 10}
Output: 3 

天真的方法是线性遍历数组并找出最小的数字。因此,时间复杂度将为O(N)。
一种有效的方法是修改二进制搜索并使用它。将数组分为两半,使用二进制搜索检查a [mid] a [mid] ,则最小的数字位于从低到中的前半部分,否则位于第二个从+1到高的中后半部分。
算法:

while(lo > 1

    if a[mid] < a[mid+1] then hi = mid

    else lo = mid+1
} 

下面是上述方法的实现:

C++
// C++ program to find the smallest number
// in an array of decrease and increasing numbers
#include 
using namespace std;
 
// Function to find the smallest number's index
int minimal(int a[], int n)
{
    int lo = 0, hi = n - 1;
 
    // Do a binary search
    while (lo < hi) {
 
        // Find the mid element
        int mid = (lo + hi) >> 1;
 
        // Check for break point
        if (a[mid] < a[mid + 1]) {
            hi = mid;
        }
        else {
            lo = mid + 1;
        }
    }
 
    // Return the index
    return lo;
}
 
// Driver Code
int main()
{
    int a[] = { 8, 5, 4, 3, 4, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    int ind = minimal(a, n);
 
    // Print the smallest number
    cout << a[ind];
}


Java
// Java program to find the smallest number
// in an array of decrease and increasing numbers
class Solution
{
// Function to find the smallest number's index
static int minimal(int a[], int n)
{
    int lo = 0, hi = n - 1;
 
    // Do a binary search
    while (lo < hi) {
 
        // Find the mid element
        int mid = (lo + hi) >> 1;
 
        // Check for break point
        if (a[mid] < a[mid + 1]) {
            hi = mid;
        }
        else {
            lo = mid + 1;
        }
    }
 
    // Return the index
    return lo;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 8, 5, 4, 3, 4, 10 };
    int n = a.length;
    int ind = minimal(a, n);
 
    // Print the smallest number
    System.out.println( a[ind]);
}
}
//contributed by Arnab Kundu


Python3
# Python 3 program to find the smallest
# number in a array of decrease and
# increasing numbers
 
# function to find the smallest
# number's index
def minimal(a, n):
     
    lo, hi = 0, n - 1
     
    # Do a binary search
    while lo < hi:
         
        # find the mid element
        mid = (lo + hi) // 2
         
        # Check for break point
        if a[mid] < a[mid + 1]:
            hi = mid
        else:
            lo = mid + 1
        return lo
     
# Driver code
a = [8, 5, 4, 3, 4, 10]
 
n = len(a)
 
ind = minimal(a, n)
 
# print the smallest number
print(a[ind])
 
# This code is contributed
# by Mohit Kumar


C#
// C# program to find the smallest number
// in an array of decrease and increasing numbers
using System;
class Solution
{
// Function to find the smallest number's index
static int minimal(int[] a, int n)
{
    int lo = 0, hi = n - 1;
 
    // Do a binary search
    while (lo < hi) {
 
        // Find the mid element
        int mid = (lo + hi) >> 1;
 
        // Check for break point
        if (a[mid] < a[mid + 1]) {
            hi = mid;
        }
        else {
            lo = mid + 1;
        }
    }
 
    // Return the index
    return lo;
}
 
// Driver Code
public static void Main()
{
    int[] a = { 8, 5, 4, 3, 4, 10 };
    int n = a.Length;
    int ind = minimal(a, n);
 
    // Print the smallest number
    Console.WriteLine( a[ind]);
}
}
//contributed by Mukul singh


PHP
> 1;
 
        // Check for break point
        if ($a[$mid] < $a[$mid + 1])
        {
            $hi = $mid;
        }
        else
        {
            $lo = $mid + 1;
        }
    }
 
    // Return the index
    return $lo;
}
 
// Driver Code
$a = array( 8, 5, 4, 3, 4, 10 );
$n = sizeof($a);
$ind = minimal($a, $n);
 
// Print the smallest number
echo $a[$ind];
 
// This code is contributed
// by Sach_Code
?>


Javascript


输出:
3

时间复杂度:O(Log N)
辅助空间:O(1)