题目如下:

解题思路:本题的关键是找出从升序到降序的转折点。开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列。对于数组中每一个元素的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. 性能测试工具之WebBench

    一.简介 WebBench是一款在Linux下使用非常简单的压力测试工具.它的原理是:WebBench首先fork出多个子进程,每个子进程都循环做web访问测试.子进程把访问的结果通过pipe告诉父进 ...

  2. wcf restful 访问报错 *.svc HTTP error 404.17 - Not Found

    安装完成 iisreset,即使不重启也已经可以使用了

  3. ubuntu18.04 安装mongodb并使用Robo 3T连接Mongodb数据库

    1.前提: 系统:ubuntu18.04  64位 数据库:mongodb GUI:Robo 3T  2018.3.0 描述: mongodb 安装在局域网内的ubuntu的机子上面,  在win 下 ...

  4. Docker之单多/机容器管理

    Compose是用于定义和运行多容器Docker应用程序的工具.通过Compose,您可以使用YAML文件来配置应用程序的服务.然后,使用一个命令,就可以从配置中创建并启动所有服务. Docker-C ...

  5. Boostrap4 li列表橫向

    Boostrap3 li元素橫向: <ul class="nav navbar-nav list-inline"> <li class="list-in ...

  6. Aliyun-Centos 7 LNMP安装(最新版LNMP)

    linux装软件方式:1.源码安装:下载wget-->解压tar -zxvf -->配置 ./configure --->编译make -->安装 make install 2 ...

  7. P3588 [POI2015]PUS(拓扑排序+线段树)

    P3588 [POI2015]PUS 对于每个$(l,r,k)$,将$k$个位置向剩下$r-l-k+1$个位置连边,边权为$1$,这样就保证$k$个位置比剩下的大 先给所有位置填$1e9$保证最优 然 ...

  8. RESTful API 设计总结

    RESTful API 设计总结 @(技术-架构)[API, 规范, 设计] RESTful的接口设计风格应用的越来越广泛,包括Spring Cloud等微服务架构平台之间的调用都是以RESTful设 ...

  9. Solr安装(单机版)

    本文记录的是solr在win下安装配置使用的过程,最后将solr部署到Linux上通过远程访问.下一篇文章会介绍   solr集群搭建(SolrCloud)    的安装! Solr是基于Lucene ...

  10. Spark Streaming整合logstash + Kafka wordCount

    1.安装logstash,直接解压即可 测试logstash是否可以正常运行 bin/logstash -e 'input { stdin { } } output { stdout {codec = ...