原题链接在这里: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. Vim 入门

    Vim 简介 打开 Vim的四种模式 一些命令 插入 移动 文件 编辑 环境设置 .vimrc 更多命令 环境设置 折叠 显示 Vim 简介 Vim 是字符模式下的一种文本编辑器,不需要图形界面,它是 ...

  2. LeetCode 5198. 丑数 III(Java)容斥原理和二分查找

    题目链接:5198. 丑数 III 请你帮忙设计一个程序,用来找出第 n 个丑数. 丑数是可以被 a 或 b 或 c 整除的 正整数. 示例 1: 输入:n = 3, a = 2, b = 3, c ...

  3. 1. Spark Streaming概述

    1.1 什么是Spark Streaming Spark Streaming类似于Apache Storm,用于流式数据的处理.根据其官方文档介绍,Spark Streaming有高吞吐量和容错能力强 ...

  4. Android apk逆向:反编译,回编译,签名,打包。

    Android apk逆向:反编译,回编译,签名,打包流程. 第一步: apk 反编译. 1) 打开命令行窗口,输入java -version, 检测当前java版本,若版本较低, 则下载JAVA S ...

  5. Oracle打印输出在控制台

    SET SERVEROUTPUT ON  --必须有,不然显示不出declare LN_C number(10,0):=0;begin DECLARE LS_STR1 VARCHAR2(200); - ...

  6. 回文树/回文自动机(PAM)学习笔记

    回文树(也就是回文自动机)实际上是奇偶两棵树,每一个节点代表一个本质不同的回文子串(一棵树上的串长度全部是奇数,另一棵全部是偶数),原串中每一个本质不同的回文子串都在树上出现一次且仅一次. 一个节点的 ...

  7. yii框架中的下拉菜单和单选框

    yii中的下拉菜单: 第一种: <?= $form->field($model, 'parent_id')->dropDownList(ArrayHelper::map($data, ...

  8. 在.net core中数据操作的两种方式(Db first && Code first)

    在开发过程中我们通常使用的是Db first这种模式,而在.net core 中推荐使用的却是 code first 反正我是很不习惯这种开发模式 于是就搜寻整个微软的官方文档,终于找到了有关.net ...

  9. Tomcat组件梳理—Service组件

    Tomcat组件梳理-Service组件 1.组件定义 Tomcat中只有一个Server,一个Server可以用多个Service,一个Service可以有多个Connector和一个Contain ...

  10. Filter DSL 常用语法 -- 基本查询语法,必会

    转发自:https://www.cnblogs.com/ghj1976/p/5293250.html term 过滤 term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed ...