标题2(1) maximum number in a unimodal array (25 分)

You are a given a unimodal array of n distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element that runs in O(log n) time.

输入格式:
An integer n in the first line, 1<= n <= 10000. N integers in the seconde line seperated by a space, which is a unimodal array.

输出格式:
A integer which is the maximum integer in the array

输入样例:
7
1 2 3 9 8 6 5
结尾无空行
输出样例:
9
结尾无空行
注释:题目大概意思是,给出一组基本有序数找峰值。可以利用二分搜索分为三种情况
(1)中间的数i就是最大值,i-1<i, i>i+1;
(2) 中间的数在峰值的左边,小于峰值,i<i+1,下标右移;
(3)中间的数在峰值的右边,大于峰值,i>i+1,下标左移
特殊情况:峰值在第一个,或者是在最后一个

#include <iostream>
using namespace std;
int BinarySeach(int a[], int m,  int n);
int main()
{int a[100001];int n, m=1;cin>>n;for(int i=1;i<=n;i++)   //注意这里的起始下标是1cin>>a[i];int res=BinarySeach(a,1,n);cout<<res<<endl;return 0;
}
int BinarySeach(int a[], int m, int n)
{int mid=n/2;while(a[mid]<a[mid-1]&&mid>=1)mid--;while(a[mid]>a[mid-1]&&mid<=n)mid++;return a[mid-1];
}

2(1) maximum number in a unimodal array相关推荐

  1. leetcode 330. Patching Array | 1798. Maximum Number of Consecutive Values You Can Make

    1798. Maximum Number of Consecutive Values You Can Make | 1798. 你能构造出连续值的最大数目 https://leetcode.com/p ...

  2. 321. Create Maximum Number 解题方法详解

    321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...

  3. LeetCode 414. Third Maximum Number

    题目: Given a non-empty array of integers, return the third maximum number in this array. If it does n ...

  4. C#LeetCode刷题之#414-第三大的数(Third Maximum Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...

  5. LeetCode-321 Create Maximum Number

    题目描述 Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum ...

  6. [Swift]LeetCode414. 第三大的数 | Third Maximum Number

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. LeetCode 第201次周赛 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target

    Leetcode 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target 题目描述 思路 周赛代码 优化代码 ...

  8. 414. Third Maximum Number

    414. Third Maximum Number Given an array of integers, return the 3rd Maximum Number in this array, i ...

  9. LeetCode | 414. Third Maximum Number

    . 题目 Given an integer array nums, return the third distinct maximum number in this array. If the thi ...

  10. openstack创建实例报错Exceeded maximum number of retries

    Error: 实例 "vm2" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Exceeded maximum number of retries. Exceed ...

最新文章

  1. 服务器虚拟机密度,服务器整合:虚拟机密度大未必好事
  2. Redis整合springboot实现集群模式
  3. python与线性代数 矩阵与方程组
  4. 一起学Windows Phone7开发(十四.一 Phone Task)
  5. ffmpeg 源代码简单分析 : avcodec_register_all()
  6. 字符串相加和valueof_Java字符串valueOf()示例
  7. layoutIfNeeded 就这样把我害惨
  8. Layui中的table中toolbar自定义过程
  9. java网上零食销售网站系统
  10. 2018 秋招 百度二轮面试---血淋淋的经历写实
  11. Windows上使用VLC插件的方式在IE浏览器上播放RTSP视频流
  12. 关于Apple ID相关设置
  13. 设计模式-行为型-访问者模式
  14. C primer plus——C进阶探幽
  15. 计算机领域有哪些精妙绝伦的设计
  16. GitHub 全域数字年报:携手推动开源世界的超级协作
  17. 微雪新款HDMI液晶框架安装
  18. 什么是“UD”什么是fbinst启动盘制作?
  19. HGP03参数监视软件
  20. 计算机主机风扇声音大的原因,台式电脑风扇响声大的原因和修复方法

热门文章

  1. 【暑期每日一题】洛谷 P5708 【深基2.习2】三角形面积
  2. 网站开发进阶(六十六)CSS3 - 新单位vmin/vmax与旧单位ex/ch使用详解
  3. 如何通过IP共享文件
  4. Excel实现多表关联查询-VLOOKUP
  5. [工具使用]黑暗引擎FOFA
  6. 南阳oj 57题------6174问题
  7. 助你成为Java大神的十个建议
  8. 玩苹果为什么要越狱?
  9. 如何计算均值、标准差和标准误差
  10. 使用 Illustrator 中组合形状的方法详解