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 ...
随机推荐
- Enhancement S_ALR_87011964 Asset Balance Report to add custom column
Enhancement S_ALR_87011964 Asset Balance Report to add custom column - SAP Tutorial Include own fiel ...
- selenium最常用的基本方法
1.打开,关闭浏览器 打开chrome浏览器:webdriver.Chorme() 打开Firefox浏览器:webdriver.Firefox() 关闭当前浏览器窗口:driver.close() ...
- C++11之线程库
在 C++11 之前,涉及到多线程问题,都是和平台相关的,比如 Windows 和 Linux 下各有自己的接口,这使得代码的可移植性比较差.C++11 中最重要的特性就是对线程进行支持了,并且可以跨 ...
- twenty four
vue基础代码 <script src="vue路径"></script> <script> const vm = new Vue({ //el ...
- Android蓝牙固件升级 DFU-OTA 固件升级
1.添加 依赖包: implementation 'no.nordicsemi.android:dfu:1.11.0' 2.DfuService类继承 DfuBaseService package ...
- SSH远程服务器时报错 /bin/bash : Permission denied
SSH远程服务器时报错 /bin/bash : Permission denied 今日接到用户那边的报障,/bin/bash : Permission denied报错,用户使用的是具有sudo权限 ...
- wpf treeview 选中节点加载数据并绑定
<TreeView Grid.Row="0" Grid.Column="0" x:Name="FolderView" Canvas.T ...
- jupyter notebook相关笔记
导出pdf时隐藏代码 参考:https://segmentfault.com/q/1010000043309446
- debian/ubuntu下安装nodejs npm
举例:在 /home 目录下 wget https://nodejs.org/dist/v16.17.1/node-v16.17.1-linux-x64.tar.xz nodejs官网下载地址,目前最 ...
- 【Python3+Selenium】基本操作
一.环境准备 1.Selenium安装教程 1.1 打开cmd,输入如下命令:pip/pip3 install selenium 1.2 安装完成后查看Python路径包管理(Lib\site-pac ...