编程语言
首页 > 编程语言> > 算法第二章上机实践报告

算法第二章上机实践报告

作者:互联网

7-1 maximum number in a unimodal array (40 分)  

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
 
 

 

   这道题用了分而治之策略里面的二分法,极大的缩小了运行时间,时间复杂度为O(logn),空间复杂度为O(logn)。

  同时分而治之之策略的框架为:

         分解原问题,解决子问题,合并问题解。就是将问题细化,找到最“单纯”的子问题就比较好解题。

  另外我们要注意一些特殊情况,比如单调递增序列或者单调递减序列等等,这时问题解往往为边缘值。

 

标签:上机,复杂度,maximum,算法,logn,integer,array,第二章,its
来源: https://www.cnblogs.com/fuxiaoya/p/15487334.html