【LeetCode】845. Longest Mountain in Array 解题报告(Python)
作者: 负雪明烛
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 - 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 <= 10000
- 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)的更多相关文章
- LeetCode 845. Longest Mountain in Array
原题链接在这里:https://leetcode.com/problems/longest-mountain-in-array/ 题目: Let's call any (contiguous) sub ...
- LeetCode: Search in Rotated Sorted Array 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 【leetcode】845. Longest Mountain in Array
题目如下: 解题思路:本题的关键是找出从升序到降序的转折点.开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列.对于数组中每一个元素的mountain length就是左边升 ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
- 【LeetCode】88. Merge Sorted Array 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...
- 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- LeetCode 521 Longest Uncommon Subsequence I 解题报告
题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group o ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
随机推荐
- 45-Letter Combinations of a Phone Number
Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution Total Accepted: 7855 ...
- 充分利用nginx的reload功能平滑的上架和更新业务
以前更新我们都要停服务更新,不管什么时候更新,都可能有客户在访问,体验不好,二是如果有数据传输,可能会造成数据丢失. nginx reload可以不间断更新配置文件,原理就是当我们修改配置文件发起re ...
- dart系列之:还在为编码解码而烦恼吗?用dart试试
目录 简介 为JSON编码和解码 UTF-8编码和解码 总结 简介 在我们日常使用的数据格式中json应该是最为通用的一个.很多时候,我们需要把一个对象转换成为JSON的格式,也可以说需要把对象编码为 ...
- 学习java的第八天
一.今日收获 1.学习完全学习手册上2.3转义字符与2.4运算符两节 二.今日难题 1.没有什么难理解的问题 三.明日目标 1.哔哩哔哩教学视频 2.Java学习手册
- 日常Java 2021/10/12
封装 在面向对象程式设计方法中,封装是指-种将抽象性函式接口的实现细节部分包装.隐藏起来的方法 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问 要访问该类的代码和数据,必 ...
- Largest Rectangle in Histogram及二维解法
昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ...
- oracle extract
select extract(year from systimestamp) year ,extract(month from systimestamp) month ...
- zabbix之被动模式之编译安装proxy
#:准备源码包,编译安装 root@ubuntu:/usr/local/src# ls zabbix-4.0.12.tar.gz root@ubuntu:/usr/local/src# tar xf ...
- hash 模式与 history 模式小记
hash 模式 这里的 hash 就是指 url 后的 # 号以及后面的字符.比如说 "www.baidu.com/#hashhash" ,其中 "#hashhash&q ...
- 【Java 基础】Java动态代理
Java动态代理InvocationHandler和Proxy java动态代理机制中有两个重要的类和接口InvocationHandler(接口)和Proxy(类),这一个类Proxy和接口Invo ...