题目如下:

解题思路:本题的关键是找出从升序到降序的转折点。开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列。对于数组中每一个元素的mountain length就是左边升序的子序列长度加上右边降序的左序列长度。对于右边降序,如果我们遍历数组,那么降序就是升序。那么我们要做的就是,分别正序和倒序遍历数组,求出每个元素的左边和右边升序的子序列长度,那么这个长度的和就是答案。如下图,我们分别用两个数组保持正序和逆序遍历时候,每个元素在当前所属的递增子序列中的长度位置,如果非递增则记为1。求出所有元素的长度位置后,对于任意一个元素,其mountain length = dp[正序] + dp[倒序] - 1 (dp[正序] != 1 && dp[倒序] != 1)。

代码如下:

class Solution(object):
def longestMountain(self, A):
"""
:type A: List[int]
:rtype: int
"""
dp_order = [1 for x in xrange(len(A))]
dp_reverse = [1 for x in xrange(len(A))]
for i in xrange(1,len(A)):
if A[i] > A[i-1] :
dp_order[i] = dp_order[i-1] + 1 for i in xrange(-1,-len(A),-1):
if A[i] < A[i-1] :
dp_reverse[i-1] = dp_reverse[i] + 1 res = 0
for v1,v2 in zip(dp_order,dp_reverse):
if v1 != 1 and v2 != 1:
res = max(res,v1+v2-1) #print dp_order
#print dp_reverse
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】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  3. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  4. LeetCode 845. Longest Mountain in Array

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

  5. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  6. 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

    [题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  7. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  8. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

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

  9. 【leetcode】941. Valid Mountain Array

    题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...

随机推荐

  1. Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它

    Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它 TFmxObject 增加了 TagObject.TagFloat.TagString, 算 ...

  2. Windows下对函数打桩,及Linux类似技术

    一个简单的桩实现类: #define JMPCODE_LENGTH 5 //x86 平坦内存模式下,绝对跳转指令长度 #define JMPCMD_LENGTH 1 //机械码0xe9长度 #defi ...

  3. linux使用ltrace和strace跟踪程序执行过程

    yum install strace yum install ltrace 1.strace  ping -c 1 www.baidu.com 2.ltrace  ping -c 1 www.baid ...

  4. Numpy——进阶篇

    impoort numpy as np arr=np.arange(10) #输出奇数 arr[arr%2==1] #将arr中的所有奇数替换为-1,而不改变arr out=np.where(arr% ...

  5. MySQL数据类型之整型

    还一个   Decimal 就是这么创建 查看当前数据表 查看tb1得表得所有记录....

  6. [Web 前端] 027 jQuery 相关尺寸与事件绑定

    1. 相关尺寸 1.1 获取元素相对于文档的偏移量 var pos = $('#small').offset(); console.log(pos.left, pos.top); 1.2 获取当前元素 ...

  7. [转帖]深入理解 MySQL—锁、事务与并发控制

    深入理解 MySQL—锁.事务与并发控制 http://www.itpub.net/2019/04/28/1723/ 跟oracle也类似 其实所有的数据库都有相同的机制.. 学习了机制才能够更好的工 ...

  8. java int转Short

    使用short(xx) problemMultipleChoiceDO.setExamCount((short)0);//在数据库中是smallint类型

  9. 画一个心送给心爱的小姐姐,Python绘图库Turtle

    Python绘图库Turtle Turtle介绍 Turtle是Python内嵌的绘制线.圆以及其他形状(包括文本)的图形模块. 一个Turtle实际上是一个对象,在导入Turtle模块时,就创建了对 ...

  10. git学习指南

    近来学习Git,苦寻资料下发现廖雪峰老师的教程很好,在此推荐传送门 附每节总结,方便查阅 创建版本库 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 使用命令git ...