【leetcode】845. Longest Mountain in Array
题目如下:

解题思路:本题的关键是找出从升序到降序的转折点。开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列。对于数组中每一个元素的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的更多相关文章
- 【LeetCode】845. Longest Mountain in Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- LeetCode 845. Longest Mountain in Array
原题链接在这里:https://leetcode.com/problems/longest-mountain-in-array/ 题目: Let's call any (contiguous) sub ...
- 【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 ...
- 【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 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】941. Valid Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】941. Valid Mountain Array
题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...
随机推荐
- Octavia 创建 loadbalancer 的实现与分析
目录 文章目录 目录 从 Octavia API 看起 Octavia Controller Worker database_tasks.MapLoadbalancerToAmphora comput ...
- Delphi驱动方式WINIO模拟按键 可用
http://www.delphitop.com/html/yingjian/152.html Delphi驱动方式WINIO模拟按键 作者:admin 来源:未知 日期:2010/2/1 1:14: ...
- 2018.03.29 python-pandas 数据读取
#数据读取# read_table,read_csv,read_excel #读取普通分隔数据:read_table #可以读取txt,csv import os import pandas as p ...
- web form 防止一个请求重复提交
/// <summary> /// 防止一个请求重复提交 /// </summary> public void PreventRepeatSubmit() { if (Scri ...
- Java与C#不同
1.C#方法定义可以有默认参数,而Java则不支持该方式. C#方法定义 public void ShowMessage(string text,string orderId="" ...
- 最小二乘法公式推导及Python实现
机器学习使用线性回归方法建模时,求损失函数最优解需要用到最小二乘法.相信很多朋友跟我一样,想先知道公式是什么,然后再研究它是怎么来的.所以不多说,先上公式. 对于线性回归方程\(f(x) = ax + ...
- 手把手教你SOAP访问webservice并DOM解析返回的XML数据(转)
http://blog.csdn.net/u012534831/article/details/51357111 前言: 目前我们项目组还在采用webservice这种http方式,并且某些网站服务提 ...
- 多线程18-QueueUserWorkItem
)); } ; ; )); ThreadPool.QueueUserWorkItem(A ...
- kubeadm初始化kubernetes集群
有两种方式安装集群: 1.手动安装各个节点的各个组件,安装极其复杂困难. 2.使用工具:kubeadm kubeadm 是官方提供的专门部署集群的管理工具. 1. 在kubeadm下每个节点都需要安装 ...
- Centos7Yum安装PHP7.2流程
Centos7Yum安装PHP7.21.安装源 安装php72w,是需要配置额外的yum源地址的,否则会报错不能找到相关软件包. php高版本的yum源地址,有两部分,其中一部分是epel-relea ...