LeetCode 845. Longest Mountain in Array
原题链接在这里: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 - 1such thatB[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:
0 <= A.length <= 100000 <= 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的更多相关文章
- 【LeetCode】845. Longest Mountain in Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...
- 【leetcode】845. Longest Mountain in Array
题目如下: 解题思路:本题的关键是找出从升序到降序的转折点.开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列.对于数组中每一个元素的mountain length就是左边升 ...
- [LeetCode] Longest Mountain in Array 数组中最长的山
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
- [Swift]LeetCode845. 数组中的最长山脉 | Longest Mountain in Array
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
- Longest Mountain in Array 数组中的最长山脉
我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [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 ...
- 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 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
随机推荐
- 长乐国庆集训Day3
T1 动态逆序对 题目 [题目描述] 给出一个长度为n的排列a(1~n这n个数在数列中各出现1次).每次交换两个数,求逆序对数%2的结果. 逆序对:对于两个数a[i],a[j](i<j),若a[ ...
- c++11多线程记录2:线程管理
线程没有调用join和detach thread对象必须调用join或者detach,否则程序会终止 例如: void func() { std::cout << "hello, ...
- docker stack 部署 mssql
=============================================== 2019/12/8_第1次修改 ccb_warlock == ...
- SSM 前后端分离 这里controll层的返回值和之前那个不一样
1.先创建实体类: 2.创建mapper层 package cn.kgc.mapper; import cn.kgc.Account;import org.apache.ibatis.annotati ...
- 利用jQuery-Word-Export导出word (含ECharts)
写在前面的话:写博客的初衷是想把自己学到的知识总结下来,在写的过程中,相当于又把知识梳理了一遍.我坚信有输入,有输出,技术才会进步.我一般都会自己写一个小demo,测试没有问题,再进行整理. 在实 ...
- python-tyoira基本
目录 .Typora安装 我们在之前的时候记录笔记就是使用word和记事本,但是从今天开始我们要更换软件,记录笔记使用Typora软件,为什么要使用Typora的软件呢,是因为我们程序员不只是写代码这 ...
- Java8stream表达式
// 输出:hello System.out.println(Optional.ofNullable(hello).orElse("hei")); // 输出:hei System ...
- 2019-07-22 phpStudy配置虚拟主机
1.右击 phpStudy ->[打开配置文件]->[vhosts-conf]: 2.在里面加入如下代码,并保存: NameVirtualHost *:80 <VirtualHost ...
- Web网站实现Google登录
一.打开谷歌控制台:https://console.developers.google.com/apis 二.点击创建凭据,如下图,填写项目地址等 三.创建好客户端ID和秘钥后,填写对应的项目网址和登 ...
- 使用EwoMail搭建属于自己的个人邮件服务器——超详细图文教程
版权声明:本文为CSDN博主「C_成」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_41692307 ...