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的更多相关文章

  1. [LeetCode] Longest Mountain in Array 数组中最长的山

    Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...

  2. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. [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 ...

  5. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  6. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  7. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. python之目录结构01

    本文档主要是自己学习巩固以及复习之用,主要写些自己的学习体会! 以下为一个简要的目录构: Foo/ |-- bin/ | |-- foo | |-- foo/ | |-- tests/ | | |-- ...

  2. 关于ecplipse中的中文都成乱码的问题

    这个问题之前也搞死我了,差不多搞了两个下午才搞好 唉,说多了都是泪 时间过的有点久,不是很记得了,不过我这个问题是装fx包之前发生的,后来我是改了jdk版本的所以可能会有些不同 首先,中文会变成乱码主 ...

  3. 使用Git GUI Here进行推送时产生报错

    许多小伙伴在刚使用git时都会遇到这个问题,在推送一次内容之后,想要再次推送新的数据产生报错 下面就是我们的错误提示: 我们需要先把数据进行更新 找到Remote-->Fetch from--& ...

  4. Unity 关于可寻址资源系统Addressables的使用和理解(一) 准备工作

    一.打开Unity的PackageManager,安装Addressables包 二.打开分组面板,对未来要分类的资源包进行分组,并对组进行设置. 1.菜单栏选择Window/AssetManagen ...

  5. 面试之CAS

    1.CAS(Compare And Swap)比较并替换,是线程并发运行时用到的一种技术或者算法,CAS与之对应的是一些锁技术,,例如synconozied,同事这种比较替换的思想也可以运用到数据库上 ...

  6. vi 快捷键/ctags

    vi 配置 syntax enableset nu set relativenumberset hlsearch set autoindentset shiftwidth=4set tabstop=4 ...

  7. SignalR 的应用

    一.应用场景: 在项目中有一个地方需要定时查询数据库是否有数据,如果有则显示在界面上. 二.可以使用ajax定时查询来做: var inter = window.setInterval(refresh ...

  8. Markdown 基础语法 备忘录

    在vscode中使用Markdown先要安装一些插件: Markdown Preview Enhanced Paste Image LimfxCodeEX Code Spell Checker 一级标 ...

  9. JAVA仓库管理系统(附源码+调试)

    JAVA仓库管理系统--三只松鼠仓库管理系统功能描述(1)登录模块:登录信息等存储在数据库中(2)基本档案管理模块:供货商管理,销售商管理,货品档案管理,仓库管理(3)采购订货模块:用户可以通过查询条 ...

  10. dnspy查看async方法