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 ...
随机推荐
- [笔记]windows cmd常用命令
1.返回上一级目录 目前似乎没有直接的命令,参考 https://stackoverflow.com/questions/48189935/how-can-i-return-to-the-prev ...
- react useGetObjState
import {useCallback, useRef, useState} from 'react';function useGetObjState(initialState = {}) { con ...
- windows 安装mysql-8.0.13(zip安装)
安装环境说明 系统版本:windows10 mysql版本:mysql-8.0.13-winx64.zip 下载地址:http://mirrors.163.com/mysql/Downloads/My ...
- logback-spring.xml配置说明
logback-spring.xml的配置说明 <?xml version="1.0" encoding="UTF-8"?> <configu ...
- mount无响应
mount -t xfs /dev/sdb /data 挂载不成功,且命令无任何回显. dmesg 查看到有报错 tailf /var/log/messages -n 100 systemctl da ...
- node_modules/canvas npm ERR! command failed
Installing packages. This might take a couple of minutes.Installing react, react-dom, and react-scri ...
- js中常用的运算符
1. ?. 链接运算符 特性: 一旦遇到空置就会终止 例子: let name = obj?.name persion.getTip?.() // 没有getTip 方法则不会执行 2. ?? 空值合 ...
- Two-Stream Consensus Network for Weakly-Supervised Temporal Action Localization概述
1.针对的问题 在没有帧级注释的情况下,W-TAL方法很难识别假阳性的动作建议,并生成具有精确时间边界的动作建议.具体来说,之前的W-TAL方法所面临的最关键的问题之一是缺乏排除假阳性动作建议的能力. ...
- String.prototype.replace--替换字符串
str.replace(regexp|substr, newSubStr|function) API本身不改变原本的字符串,只是返回新的字符串例子:用函数作为第二个参数function rep ...
- zabbix 监控域名到期时间
cat userparameter_http.conf UserParameter=http_discovery,/usr/bin/python /etc/zabbix/scripts/base/ht ...