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 ...
随机推荐
- B站动态转发抽奖脚本+教程
运行python脚本需要的条件: 1.连通的网络 2.已安装Python2并配置环境变量 3.Python脚本源码 环境搭建: 网络就不用我说了("'▽'") 那么下面我们来安装 ...
- Go基础编程实践(十)—— 数据库
从数据库中读取数据 在http://sqlitebrowser.org/下载sqlite3可视化工具,在本main.go同目录下创建personal.db数据库,创建表如下: package main ...
- RHEL6.5 移植使用CentOS 的YUM 步骤
问题:使用 Red Hat Enterprise Linux Server(RHEL) yum安装软件时显示 This system is not registered with RHN. RHN s ...
- C#-Windows服务创建和运行
Windows服务创建和运行 适用场景: ASP.Net通常是一个无状态的提供程序,不支持持续运行代码或者定时执行某段代码,所以我们需要构建自己的Windows服务来运行那些定时任务. 项目中需 ...
- catch socket error
whois_handler.dart import 'dart:io'; import 'package:async/async.dart'; import 'dart:convert'; class ...
- vue-quill-editor回显时移除焦点
直接复制开用 解决在回显数据的时候会默认聚焦 this.$refs.myQuillEditor.quill.enable(false); setTimeout(() => { this.$ref ...
- redhat7.2下VNC没法显示图像
1,Symptom /root/.vnc/HR-ECC-PRD-02:1.log内容有信息如下: VNCSconnST: Server default pixel fromat depth 24 (3 ...
- 安装python工具
安装python编写工具 本篇幅只限于安装linux系统上. 想要实现linux程序在windows桌面上打开,需要在linux上打开X11forward和在windows上安装Xmanager ID ...
- golang之reflection
反射就是程序能够在运行时检查变量和值,求出它们的类型. reflect包实现运行时反射. 创建一个接收任何数据类型任何数值的查询string: func createQuery(q interface ...
- pyecharts绘制geo地图
pyecharts是一种非常强大的绘图python库,绘制的图形非常好看,并且有代表性,不仅仅是地图,还可以绘制条形图.饼图.词云图等等. # 安装方法 pip install pyecharts # ...