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 ...
随机推荐
- 【运维】解决composer update出现的Discard changes [y,n,v,d,s,?]的问题
在PHP项目中,composer是一个使用非常普遍的包管理工具,在本地开发的时候出现了这个问题一搬来说问题不大,可以人为进行输入交互,但是如果是自动化发布中出现,就会等待输入导致卡住,是一个需要解决的 ...
- 修改系统hosts文件访问github
C:\Windows\System32\drivers\etc 199.232.69.194 github.global.ssl.fastly.net 140.82.114.4 github.com
- 服务器5M带宽下载速计算
5M贷款的服务器实际下载速度不是5M每秒,而是640KB/S,是由于服务商口中的宽带指的是bit(比特),而下载速度使用的单位是Byte(字节),1Byte(字节)=8bit(比特),所以,宽带和下载 ...
- Hyperledger fabric 2.2.0 环境搭建
基础环境搭建 ### docker 安装 如果服务器上有旧版的docker,需要先执行卸载操作. $ sudo yum remove docker \ docker-common \ docker-s ...
- FastReport 单元行自动换行 Table Object AutoSize
FastReport 官方实例138.fr3 有关于单元行自动换行的描述.The table object can grow depends on cells content. Notes:- set ...
- GSON 特殊类型支持序列化和反序列化,如LocalDateTime
GSON 特殊类型支持序列化和反序列化,如LocalDateTime DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern ...
- django项目中使用swagger来实现接口文档自动生成
一.Swagger 一般我们在对接前后端的时候,都需要提供相应的接口文档.对于后端来说,编写接口文档即费时费力,还会经常因为没有及时更新,导致前端对接时出现实际接口与文档不一致.而且手写接口文档还容易 ...
- lg8862题解
脑抽了,一开始想着扫描线然后用线段树求历史最大值.
- Android-AccessibilityService
概述 AccessibilityService用于提供辅助功能服务,其在后台运行,并在触发AccessibilityEvents时由系统接收回调.此类事件表示用户界面中的某些状态转换,例如,焦点更改, ...
- 复制 GUI 状态
FORM frm_set_pf_status USING pt_extab TYPE slis_t_extab. *--·状态 'STANDARD'是从系统功能组 KKBL GUI状态下的" ...