Longest Peak
refer to: https://www.algoexpert.io/questions/Longest%20Peak
Problem Statement

Sample

Analysis

Code
1 def longestPeak(array):
2 # Write your code here.
3 longestPeakLength = 0
4 i = 1 # the first one(index 0) can not be the peak
5 while i < len(array) - 1: #check peak element
6 isPeak = array[i-1] < array[i] and array[i] > array[i+1]
7 if not isPeak:
8 i += 1
9 continue
10 leftIdx = i - 2 # expand the left side around peak
11 while leftIdx >=0 and array[leftIdx] < array[leftIdx + 1]:
12 leftIdx -= 1
13 rightIdx = i + 2# expand the right side around peak
14 while rightIdx < len(array) and array[rightIdx] < array[rightIdx - 1]:
15 rightIdx += 1
16
17 currentPeakLength = rightIdx - leftIdx - 1 # calculate the length of current peak
18 longestPeakLength = max(longestPeakLength, currentPeakLength) # update the longest length of peak
19 i = rightIdx # update i, inside the rightIdx will be the part of calculated peak, we don't need to recalculate
20 return longestPeakLength
21
Time and Space complexity
Time: O(N)-> outer loop. iterate the array once, inner loop, every element will be checked at most two or three times.(2N + 3N) -> O(N)
Space: O(1)
Longest Peak的更多相关文章
- [LeetCode] Longest Mountain in Array 数组中最长的山
		
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
 - LeetCode[3] Longest Substring Without Repeating Characters
		
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
 - 最长回文子串-LeetCode 5 Longest Palindromic Substring
		
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
 - [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
		
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
 - leetcode--5. Longest Palindromic Substring
		
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
 - [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
		
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
 - [LeetCode] Longest Palindrome 最长回文串
		
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
 - [LeetCode] Longest Absolute File Path 最长的绝对文件路径
		
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
 - [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
		
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
 - [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
		
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
 
随机推荐
- uniapp开发的app打开微信小程序
			
第一种 <script> export default { data() { return { sweixin: null } }, onLoad() { this.getPlus() } ...
 - 使用MVC编程模型通过flask蓝图实现前端后台新闻发布系统
			
相关知识点: flask:是Python开发轻量级框架,也能很好的开发动态网站. 蓝图:flask中蓝图能很好的实现代码分割管理,从而不使代码全部放在app.py杂乱无章,蓝图就像动物管理员一样,把猫 ...
 - Markdown 基础语法 备忘录
			
在vscode中使用Markdown先要安装一些插件: Markdown Preview Enhanced Paste Image LimfxCodeEX Code Spell Checker 一级标 ...
 - win10 右键添加cmd当前目录打开
			
新建.txt粘贴以下命令保存修改扩展名为.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\Ope ...
 - svn  报 is not a working copy 错误
			
当时提交代码 svn 报 is not a working copy ,上网查找问题 要我重新拉代码下来 然后放进修改的代码重新提交,我觉得很不合理,我看了下我提交的代码文件有80多个,我在想是否 ...
 - python torch  解决OSError: [WinError 126] 找不到指定的模块。 Error loading "D:\Anaconda3\lib\site-packages\torch\lib\asmjit.dll" or one of its dependencies.(安装完torch模块后出现找不到指定模块的问题)
			
昨天安装了一下python的torch模块,然后出现了以下错误: 根据叙述是因为dll文件,后来 经过我的思考...,升级了一下numpy库,就没有问题了. 根据叙述是因为dll文件,后来 经过我的思 ...
 - 算法图解 - 第1章 二分查找 与大O
			
例子:猜一个1到100之间的数,最多猜几次? # 最糟糕的猜法:一个一个的猜 - 最多查找次数: n - 运行时间: O(n) # 二分查找:在有序的一组数中猜一个数,对半猜.找到返回其位置(索引) ...
 - 字体样式font-style
			
font-family 指的是字体比如 "宋体","微软雅黑"等等 具体字体:微软雅黑.宋体.黑体.宋体等等 字体系列:sans-serif.serf.mono ...
 - 统计学习导论之R语言应用(二):R语言基础
			
统计学习导论(ISLR) 参考资料 The Elements of Statistical Learning An Introduction to Statistical Learning 统计学习导 ...
 - 暑假学习6 hdfs shell命令
			
命令行操作:cli Hadoop的命令shell : Hadoop fs -ls file: 操作 本地的文件系统 hadoop fs -ls hdfs://nod ...