作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/longest-mountain-in-array/description/

题目描述

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?

题目大意

找出一个数组中最长的山形数组的长度。所谓山形数组就是前半段是单调递增的,后半段是单调递减的。

解题方法

双数组

题目做多了之后会发现是有共性的,比如这个题和926. Flip String to Monotone Increasing就很神似,926题是把数组变成单调递增的,需要统计每个数字位置前面的0的个数和后面的1的个数。而这个题需要我们统计每个位置前面的递增数组的个数和后面递减数组的和。

最简单的方法就是使用两个数组,第一个数组是inc数组,记录的是到目前位置的最长递增连续子数组的长度;第二个数组是dec数组,记录的是当前最长递减连续子数组的长度。所以我们最后需要再次遍历这个数组一次,求当前位置inc和dec的和,还需要减去1,得到的就是当前位置为山峰的最长山形数组。

解释下为什么需要减去1:因为我们inc和dec数组初始化为1,也就是说,我们默认每个位置的数组都是一个长度为1的递增/递减数组。这个想法很显然的,因为如果有两个数字的话就是长度为2的递增或者递减数组。

举例说明:

输入:[2,1,4,7,3,2,5,1]
inc: [1, 1, 2, 3, 1, 1, 2, 1]
dec: [2, 1, 1, 3, 2, 1, 2, 1]

所以当我在求山形数组的时候把inc或者dec为1的位置过滤掉了,因为该位置为1说明这个位置前面或者后面没有跟他构成递增或者递减的数组,那肯定也不是一个山形。

时间复杂度是O(3N),空间复杂度是O(N).

class Solution(object):
def longestMountain(self, A):
"""
:type A: List[int]
:rtype: int
"""
N = len(A)
inc = [1] * N
dec = [1] * N
for i in range(1, N):
if A[i] - A[i - 1] > 0:
inc[i] = inc[i - 1] + 1
for i in range(N - 2, -1, -1):
if A[i] - A[i + 1] > 0:
dec[i] = dec[i + 1] + 1
res = 0
for i in range(1, N - 1):
if inc[i] != 1 and dec[i] != 1:
res = max(res, inc[i] + dec[i] - 1)
return res

参考资料

https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-845-longest-mountain-in-array/

日期

2018 年 10 月 26 日 —— 项目验收结束了!

【LeetCode】845. Longest Mountain in Array 解题报告(Python)的更多相关文章

  1. LeetCode 845. Longest Mountain in Array

    原题链接在这里:https://leetcode.com/problems/longest-mountain-in-array/ 题目: Let's call any (contiguous) sub ...

  2. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  3. 【leetcode】845. Longest Mountain in Array

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

  4. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  5. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  6. 【LeetCode】88. Merge Sorted Array 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...

  7. 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...

  8. LeetCode 521 Longest Uncommon Subsequence I 解题报告

    题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group o ...

  9. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

随机推荐

  1. 7. Minimum Depth of Binary Tree-LeetCode

    难度系数:easy /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  2. 学习java的第六天

    一.今日收获 1.开始了学习手册第二章的学习 2.了解了java里的常量与变量以及数据类型,与c语言的内容类似 二.今日难题 1.都是基础知识,没有什么难题 三.明日目标 1.继续学习java学习手册 ...

  3. Slay 全场!Erda 首次亮相 GopherChina 大会

    来源|尔达 Erda 公众号 相关视频:https://www.bilibili.com/video/BV1MV411x7Gm 2021 年 6 月 26 日,GopherChina 大会准时亮相北京 ...

  4. day17 阶段测验

    题目 1.找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写 有以下几种方法: [root@localhost ~]# grep -iE "^s" /pro ...

  5. day09 文件属性

    day09 文件属性 昨日回顾 yum底层原理: 第一步:执行yum install nginx安装命令 第二步:yum去/etc/yum.repos.d这个目录中 第三步:根据/etc/yum/re ...

  6. 20. VIM命令操作技巧

    V可视化选中当前行,根据光标可多行 ctrl+v 可视化块 v可视化根据光标 行间移动 快速增删改查 d 0 删除当前位置到行首 d $ 删除当前位置到行尾 d  t  (" ] ) )符号 ...

  7. pyqt5 改写函数

    重新改写了keyPressEvent() class TextEdit(QTextEdit): def __init__(self): QtWidgets.QTextEdit.__init__(sel ...

  8. Shell学习(七)——sort、uniq、cut、wc命令详解

    Shell学习(七)--sort.uniq.cut.wc命令详解 转自:[1]linux sort,uniq,cut,wc命令详解 https://www.cnblogs.com/ggjucheng/ ...

  9. Linux基础命令---mailq显示邮件队列

    mailq mailq指令可以显示出待发送的邮件队列. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法       mailq   2.选项参数列表 ...

  10. mysql与clickhouse的字段类型对应表