【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 ...
随机推荐
- linux查看硬盘信息
1. 通过内核查看硬盘信息 cat /sys/block/sda/device/model cat /sys/block/sda/device/vendor 2. 用过hdparm命令查看 hdpar ...
- SAP屏幕事件的控制
1. INITALIZATION事件 该事件在屏幕未显示之前执行,对程序设置值及屏幕元素进行初始化赋值. REPORT Y001. PARAMETERS QUAL_DAY TYPE D DEFAUL ...
- python获取csv文本的某行或某列数据
#coding = 'utf-8' import csv # 使用list,只能读取列,而且是全文读取,csv.reader会自动把CSV内容生成数组 ''' df = csv.reader(open ...
- python+selenium元素定位之XPath学习01
参考文档1:https://www.w3school.com.cn/xpath/xpath_syntax.asp 参考文档2:https://www.runoob.com/xpath/xpath-tu ...
- selenium学习-对当前浏览器窗口截屏
方法:get_screenshot_as_file(filename) # coding=UTF-8 #16.对当前浏览器窗口截屏 import sys reload(sys) sys.setdefa ...
- GmSSL Build with VS2017
使用背景: 最近研究GB35114, 有关于sip协议部分,exosip的已经编译过,由于gb3511中采用的是国密算法,因此这里记录一下GMSSL在windows下的编译过程以及遇到的错误 详细GM ...
- Linux下libaio的一个简单例子
转载:http://www.cnblogs.com/aLittleBitCool/archive/2011/10/18/2216646.html 异步io,很好玩的一个东西,从接口来看,封装的比较厉害 ...
- [LeetCode] 103. 二叉树的锯齿形层次遍历
题目链接 : https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/ 题目描述: 给定一个二叉树,返回其节 ...
- Python 入门之 内置模块 -- time模块
Python 入门之 内置模块 -- time模块 1.time模块 time翻译过来就是时间,这个模块是与时间相关的模块 import time # 内置模块 -- 标准库 (1)time.ti ...
- HNUSTOJ-1698 送外卖(TSP问题 + 状态压缩DP)
1698: 送外卖 时间限制: 1 Sec 内存限制: 128 MB提交: 123 解决: 28[提交][状态][讨论版] 题目描述 在美团和饿了么大行其道的今天,囊中羞涩的小周和小美,也随大流加 ...