题目如下:

解题思路:本题的关键是找出从升序到降序的转折点。开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列。对于数组中每一个元素的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. windows一次无线网卡被关闭事件

    使用的是Dell的笔记本,fn+无线图标莫名的是蓝牙的启动. 有一天突然无线网卡无法上网,无法发现无线网络: 1. 在服务中开启"Wired AutoConfig"以及“WLAN ...

  2. Android:adb shell 命令详解

    安卓系统是基于Linux系统开发,也就支持常见的Linux的命令,这些命令都保存在手机“/system/bin”的目录下,通过adb shell 便可以调用这些命令. 进入“/system/bin”该 ...

  3. Fabric CA/数字证书管理

    MSP(Membership Service Provider)成员管理服务提供商 名词: 1.CSR(Cerificate Signing Request):证书签署请求文件 CSR里包含申请者的 ...

  4. Mac020--常用插件

    Google浏览器常用插件 1.github插件octotree 2.掘金Chrome网上应用商店 2-1.掘金/老司机的神兵利器 2-2.好用的Google插件:来自掘金 3.Gliffy Diag ...

  5. Vim常用的功能命令

    一.编辑 查看行号     :set nu 删除一整行   dd 删除1到10行     :1,10d 删除所有内容     dG 当前行下插入一空行   o 撤销改动    u 查看当前行信息    ...

  6. linux 正则表达式 使用grep命令

    最常应用正则表达式命令是 awk sed grep [root@MongoDB ~]# cat mike.log I am mike! I like linux. I like play footba ...

  7. mysql 修改表字段默认值

    alter table 表名 alter column 字段名 drop default; (若本身存在默认值,则先删除) alter table 表名 alter column 字段名 set de ...

  8. GUI自动化测试中优化测试用例思维方法

    1.测试脚本与数据解耦(数据驱动) 让操作相同但是数据不同的测试可以通过同一 套自动化测试脚本来实现,只是在每次测试执行时提供不同的测试输入数据. 2.页面对象模型(POM) 以页面为单位来封装页面上 ...

  9. MySQL explain,type分析(转)

    问题:explain结果中的type字段代表什么意思? MySQL的官网解释非常简洁,只用了3个单词:连接类型(the join type).它描述了找到所需数据使用的扫描方式. 最为常见的扫描方式有 ...

  10. SGU 521 North-East ( 二维LIS 线段树优化 )

    521. "North-East" Time limit per test: 0.5 second(s)Memory limit: 262144 kilobytes input: ...