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 ...
随机推荐
- 【git】git子模块操作-从子模块的远端拉取上游修改 & 从项目远端拉取更改
1.从子模块的远端拉取上游修改 1.1 在项目子模块目录中,运行git fetch与git merge更新本地代码 (1) 这种方法在获取子模块非master分支的更新时,没成功获取更新,没搞清楚原因 ...
- CANas分析软件,DBC文件解析,CAN报文分析,仿CANoe曲线显示
2023.01.01:增加对Kvaser的支持参考了CANoe写了下面的软件,主要用途是对报文的回放及曲线的分析. 1.CAN连接,支持周立功CAN.CANFD及PCAN 2.DBC解析与生成文件 打 ...
- 前端vue的JsPDF html2canvas 生成pdf并以文件流形式上传到后端(转载)
原文地址 1.首先在文件内引入htmlToPdf.js这里代码引入了html2canvas和jspdf//需要 npm i html2Canvas 和 npm i jspdf 在这里将getPdf 这 ...
- C# List 分页代码
List<int> testList = new List<int>();for (int i = 1; i <= 23; i++){ testList.Add(i);} ...
- svn 报 is not a working copy 错误
当时提交代码 svn 报 is not a working copy ,上网查找问题 要我重新拉代码下来 然后放进修改的代码重新提交,我觉得很不合理,我看了下我提交的代码文件有80多个,我在想是否 ...
- 【C++复习】第八章 多态性(1)(多态类型、运算符重载)
1.多态性 1.1 什么是多态? 多态是指相同消息被不同对象接收后导致不同的行为,所谓消息是指对类成员函数的调用,不同的行为是指不同的实现,也就是调用了不同的函数. 消息在C++编程中指的是对类的成员 ...
- 【剑指Offer】【链表】反转链表
题目:输入一个链表,反转链表后,输出新链表的表头. A:定义3个结点,pNode作移动指针,pRet作输出指针,pPrev作前驱指针 在pNode没有到达链尾之前,循环里创建pNext指针记录p ...
- echarts的label的formatter 自动换行
1.加 \n 2.使用extraCssText
- React中使用CSS的N种方式
1.在组件中直接使用style,注意,div1各个属性值加双引号 const div1 = { width: "300px", margin: "30px auto&qu ...
- 利用Comparator对枚举类型进行排序的实现
1. 利用BeanComparator对List<Object>根据属性进行排序 String daXiaoOrder[] = {"很小","小", ...