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 ...
随机推荐
- Nlog连接密码隐藏
- PID模板
typedef struct{ float Kp,Ki,Kd; float Target; float Current; float Error[3]; float DeadZone; float O ...
- CAD中相交线怎样打断?CAD打断相交线步骤
在CAD设计过程中,如果想要打断图纸中相交线该如何操作呢?大家第一个想到的是不是CAD打断命令?没错,CAD打断命令是可以实现的,但是过于麻烦,今天小编来给大家分享一个更简单的方法,那就是浩辰CAD软 ...
- 如何获取android环境自带的jar包
首先找到你需要用到的类,Ctrl 并点击跳转到这个类 跳转过来之后,找到这个类所在的包 Ctrl并点击,此时会跳转到这个包所在的jar的位置 右键class.jar并选择在文件资源浏览器打开 打开以后 ...
- Centos7 更换yum软件源
https://blog.csdn.net/zhinian1204/article/details/123975403
- elasticSearch(六)--全文搜索
数据案例 1.匹配查询 a.单词查询 执行match步骤: ·检查field类型:title字段为(analyzed)字符串,所以搜索时,title需要被分析. ·分析查询字符串:QUICK! 经过标 ...
- Hide-and-Seek: Forcing a Network to be Meticulous for Weakly-Supervised Object and Action Localization概述
0.前言 相关资料: paper 网站 论文解读(知乎,CSDN) 论文基本信息: 领域:弱监督动作定位 发表时间:ICCV2017 1.针对的问题 大多数网络只识别图像最具有鉴别力的部分,不是所有相 ...
- Java流程控制之顺序结构+选择结构
顺序结构 Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行. 顺序结构是最简单的算法结构. 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是有若干个依次执行的处理步骤组 ...
- el-pagination分页-自定义左右箭头样式
1,官方样式: 查了网上,有人说可以用slot插槽,但是试过之后,因为 不能插入多个 slot(没法定义名字做区分),所以导致左右按钮一样了.. 2,还有种方法: 利用 prev-text 和 n ...
- 有关插槽中scope的问题
暂时不理解