Longest Mountain in Array 数组中的最长山脉

我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”:

  • B.length >= 3
  • 存在 0 < i < B.length - 1 使得 B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1](注意:B 可以是 A 的任意子数组,包括整个数组 A。)

给出一个整数数组 A,返回最长 “山脉” 的长度。

如果不含有 “山脉” 则返回 0。

示例 1:

输入:[2,1,4,7,3,2,5]
输出:5
解释:最长的 “山脉” 是 [1,4,7,3,2],长度为 5。

示例 2:

输入:[2,2,2]
输出:0
解释:不含 “山脉”。

提示:

0 <= A.length <= 10000
0 <= A[i] <= 10000



解题思路:

由于只能扫描一次,所以要判断出哪些点不能作为山头,而且要能区别不同的山头(包括上坡段和下坡段),我们从头扫描数组,记录两个变量up和down,记录到当前下标i时,上坡和下坡有多长。up和down在以下两个条件成立时会被归为0。

A[i - 1] == A[i] or ( down > 0 && A[i - 1] < A[i] )

条件的意思是如果前后两个值相等A[i - 1] == A[i],那么不管是上坡还是下坡截止到当前第i个元素得到的up和down都是无效的,所以必须归为0。如果当前正在下坡路段down>0但是突然有个元素不满足下坡时后一个元素小于前一个元素的特性(A[i - 1] < A[i] ),那么就证明第i个元素已经是坡底元素的下一个元素了,不属于这个山头,所以up和down要归零。

参考代码:

 1 class Solution
 2 {
 3 public:
 4     int longestMountain(vector<int>& A)
 5     {
 6       int up = 0, down = 0;
 7       int res = 0;
 8       for (int i = 1; i < A.size(); i++)
 9       {
10           if ((down > 0 && A[i] > A[i - 1]) || (A[i] == A[i - 1])) up = down = 0;
11           up += A[i] > A[i - 1];
12           down += A[i] < A[i - 1];
13           if(up && down) res=max(res,up+down+1); // 在up和down都不为0时才进入
14       }
15       return res;
16     }
17 };        

原文地址:https://www.cnblogs.com/WPF-342201/p/11385821.html

时间: 2024-08-29 02:23:19

Longest Mountain in Array 数组中的最长山脉的相关文章

[LeetCode] Longest Mountain in Array 数组中最长的山

Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note tha

[Swift]LeetCode845. 数组中的最长山脉 | Longest Mountain in Array

Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note tha

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1:

LeetCode | 1408. String Matching in an Array数组中的字符串匹配【Python】

LeetCode 1408. String Matching in an Array数组中的字符串匹配[Easy][Python][字符串] Problem LeetCode Given an array of string words. Return all strings in words which is substring of another word in any order. String words[i] is substring of words[j], if can be o

3.分治法研究-搜索数组中的最长连续递增子集

//分治算法研究 搜索数组中的最长连续递增子集var cc=consolefunction find_max_crossing_lenarray(A,low,mid,high){    var max_left=mid,max_right=mid    var left_sum=1    var sum=0    for(var i=mid;i>low;i--){        sum=A[i]-A[i-1]        if(sum==1){            left_sum++   

[LeetCode] Kth Largest Element in an Array 数组中第k大的数字

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

215 Kth Largest Element in an Array 数组中的第K个最大元素

在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假设 k 总是有效的,1 ≤ k ≤ 数组的长度. 详见:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 方法一: class Solution { public: int findKthLarges

(Java) LeetCode 442. Find All Duplicates in an Array —— 数组中重复的数据

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

js删除Array数组中的某个元素

Array.prototype.indexOf = function (val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) return i; } return -1; }; Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }; v