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 ...
随机推荐
- centos7.6安装rz命令上传文件
centos7.6安装rz命令 1.执行安装命令:yum -y install lrzsz 2.进行上传操作:rz 跳转到本地文件选择后即可上传
- python之路5:常用模块
模块简介 time & datetime模块 random os sys shutil json & pickle shelve xml处理 configparser hashlib ...
- 路由器、集线器、交换机分别工作在OSI七层协议模型的哪一层
路由器三层(网络层): 集线器一层(物理层): 普通交换机二层(数据链路层). 现在也有工作在第三层的交换机. OSI七层网络模型由下至上为1至7层,分别为物理层(Physical layer),数据 ...
- 攻防世界-file_include(绕过base64)
有简单过滤防护的文件包含 一.源码分析 易知$filename处可能会存在文件包含漏洞.但注意到include了一个./check.php文件,猜测可能有过滤.先不管,于是直接使用普通payload ...
- ubuntu usb network card drive
通过 lsusb -t命令查看网卡型号 /: Bus 02.Port 1: Dev 1, class="root_hub", Driver=xhci_hcd/4p, 5000M | ...
- 智能风控:python金融风险管理与评分卡建模
Ch1 互联网金融的申请过程,通常由用户从移动端发起,首次贷款用户会经理申请.四要素验证.授信与额度利率定价.多层审批.用户提款等多个环节:对于还款后再次贷款的复贷客户,平台通常会基于更好的信用评估结 ...
- xd p4 WEB源码拓展
WEB 源码在安全测试中是非常重要的信息来源,可以用来代码审计漏洞也可以用来做信息突破口,其中 WEB 源码有很多技术需要简明分析. 知识点 关于 WEB 源码目录结构 后台目录.模板目录.数据库目录 ...
- 关于pytorch一些基础知识的备份
压缩conda环境用于备份目的实际上没有意义,因为还有其他方法可以做到这一点,这可能更合适,并使用专为此而设计的内置功能. 您可以创建一个环境.txt版本conda,详细说明其中的每个模块和版本,然后 ...
- c#怎样删除指定文件名的文件
我有一个文件夹,里面有6个文件,我现在要删除字母B开头的三个文件,只需要剩下A开头的文件即可用C#怎样操作??? foreach (string d in Directory.GetFileSyste ...
- STM32F4 HAL库中是如何实现UART IO配置的?
1.配置串口IO.中断等底层的东西需要在用户文件中重写HAL_UART_MspInit函数2.hal库是在stm32f4xx_hal_msp.c文件中重写的HAL_UART_MspInit函数,分析如 ...