原题链接在这里:https://leetcode.com/problems/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 that B could be any subarray of A, including the entire array A.)

Given an array A of integers, return the length of the longest mountain.

Return 0 if there is no mountain.

Example 1:

Input: [2,1,4,7,3,2,5]
Output: 5
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.

Example 2:

Input: [2,2,2]
Output: 0
Explanation: There is no mountain.

Note:

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

Follow up:

  • Can you solve it using only one pass?
  • Can you solve it in O(1) space?

题解:

Could do it with mutiple passes. One pass from left to right calculating up count.

One pass from right to left calculating down count.

Then final pass calculate maximum.

The follow up says one pass with O(1) space.

When A[i] == A[i-1], it is not mountain, i++.

First try counting up first, then try counting down. If both counts are positive, that means it is a mountain. Update res.

Time Complexity: O(n). n = A.length.

Space: O(1).

AC Java:

 class Solution {
public int longestMountain(int[] A) {
int res = 0;
int i = 1;
int n = A.length;
while(i<n){
while(i<n && A[i]==A[i-1]){
i++;
} int up = 0;
while(i<n && A[i]>A[i-1]){
up++;
i++;
} int down = 0;
while(i<n && A[i]<A[i-1]){
down++;
i++;
} if(up>0 && down>0){
res = Math.max(res, up+down+1);
}
} return res;
}
}

LeetCode 845. Longest Mountain in Array的更多相关文章

  1. 【LeetCode】845. Longest Mountain in Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...

  2. 【leetcode】845. Longest Mountain in Array

    题目如下: 解题思路:本题的关键是找出从升序到降序的转折点.开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列.对于数组中每一个元素的mountain length就是左边升 ...

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

    Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...

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

    Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...

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

    我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] ...

  6. [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  7. [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  8. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  9. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

随机推荐

  1. C语言conio.h部分解释

    #include <conio.h> int getch(void);// 从控制台得到下一个字符,以ASCII值返回,并不在屏幕显示该字符 int getche(void);// 从控制 ...

  2. python 字符串替换功能 string.replace()可以用正则表达式,更优雅

    说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...

  3. Actions require unique method/path combination for Swagger

    原文:Actions require unique method/path combination for Swagger services.AddSwaggerGen (c => { c.Re ...

  4. Bootstrap中的datetimepicker用法总结

    bootstrap时间控件参考文档(少走弯路) https://blog.csdn.net/hizzyzzh/article/details/51212867#31-format-%E6%A0%BC% ...

  5. np.any()基本用法与不一样环境中的用法

    import numpy as npa=np.ones((2,3,4))b=np.array([1,2,3])c=b<2k=np.any(c) # 是或的关系,只要有一个满足,则输出为TRUEp ...

  6. mysql-多表联查(实例)

    目录 多表查询 笛卡尔积查询 内连接查询 左外连接查询 右外连接查询 全外连接查询 多表查询 笛卡尔积查询 笛卡尔积查询:就是两张表相乘,若左边表有M条信息,右边表有N条信息,那么查询显示的信息总共为 ...

  7. vue中使用echart柱状图

    一: <template> <Layout> <Content> <Card :style="{minHeight:'300px'}"&g ...

  8. CAS 的问题

    cas这么好用,那么有没有什么问题呢?还真有 ABA问题 CAS需要在操作值的时候检查下值有没有发生变化,如果没有发生变化则更新,但是如果一个值原来是A,变成了B,又变成了A,那么使用CAS进行检查时 ...

  9. 关于Spring IOC (DI-依赖注入)你需要知道的一切

    <Spring入门经典>这本书无论对于初学者或者有经验的工程师还是很值一看的,最近花了点时间回顾了Spring的内容,在此顺带记录一下,本篇主要与spring IOC相关 ,这篇博文适合初 ...

  10. gps示例代码

    /* main.c */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #incl ...