[LintCode] 585 Maximum Number in Mountain Sequence

4/23/2017

算法班

注意返回的是值,而不是index

 1     public int mountainSequence(int[] nums) {
 2         if (nums == null || nums.length == 0) return -1;
 3         int start = 0, end = nums.length - 1;
 4         int ret;
 5
 6         while (start + 1 < end) {
 7             int mid = start + (end - start) / 2;
 8
 9             if (nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1]) return nums[mid];
10             if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
11                 end = mid;
12             } else {
13                 start = mid;
14             }
15         }
16         if (nums[start] < nums[end]) return nums[end];
17         return nums[start];
18     }
时间: 2024-10-14 18:21:20

[LintCode] 585 Maximum Number in Mountain Sequence的相关文章

[LintCode] Create Maximum Number 创建最大数

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digit

POJ2699 The Maximum Number of Strong Kings

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats

【POJ2699】The Maximum Number of Strong Kings

Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a tournament T, the score of x is the number of playe

POJ 2699 The Maximum Number of Strong Kings Description

The Maximum Number of Strong Kings Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a tournament T, th

解题报告 之 POJ2699 The Maximum Number of Strong Kings

解题报告 之 POJ2699 The Maximum Number of Strong Kings Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a t

The maximum number of cell styles was exceeded. You can define up to 4000 styles

POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时, 就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面 报错如下: Caused by: java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

Maximum number of WAL files in the pg_xlog directory (1)

Guillaume Lelarge: Hi, As part of our monitoring work for our customers, we stumbled upon an issue with our customers' servers who have a wal_keep_segments setting higher than 0. We have a monitoring script that checks the number of WAL files in the

414. Third Maximum Number

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). 给定一个非空数组的整数,返回该数组中的第三个最大数. 如果不存在,返回最大数量. 时间复杂度必须在O(n)中. Example 1: Input: [3, 2,

(leetcode题解)Third Maximum Number

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). Example 1: Input: [3, 2, 1] Output: 1 Explanation: The third maximum is 1. Examp