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. nestjs中swagger的基本使用

    nestjs中swagger的基本使用 安装 $ npm install --save @nestjs/swagger swagger-ui-express //如果使用fastify,则必须安装fa ...

  2. centos7下的apache2.4安全配置

    基本概括 关键词Server ServerRoot  "/etc/httpd"    #apache软件安装的位置 Listen 80  #监听的端口号 ServerName ww ...

  3. k8s centos 79,用kuboard-spray装成功。低版本的。安装docker-ce,安装epel源

    安装日志 #安装epel源 yum install epel-release -y --nogpgcheck # 安装docker-ce yum install -y yum-utils device ...

  4. 使用Github或Gitlab的Webhooks实现代码自动更新部署(Ubuntu20.04)

    1.安装ssh服务root@Ubuntu:~# apt-get install openssh-server 2.部署phproot@Ubuntu:~# add-apt-repository ppa: ...

  5. Devexpress WPF ChartControl 多Y轴

    <Window x:Class="DXApplication4.MainWindow" xmlns="http://schemas.microsoft.com/wi ...

  6. java面试准备基础篇

    1.Java中常用关键字和用途 synchronized: 加锁 transient 不参与序列化和反序列化 volatile 保证其他线程可见性,不保证原子性,禁止指令重排 2.hashCode() ...

  7. jjupyter notebook 修改存储位置和修改默认浏览器

    先jupyter notebook --generate-config 找到配置文件位置 打开这里的哪个文件 然后修改即可 1.修改默认打开目录找到 #c.NotebookApp.notebook_d ...

  8. 微信小程序授权及检测访问当前页面需要去登录的操作

    1.小程序授权登录 这里我直接复制代码: login.js const app = getApp() Page({ data: { //判断小程序的API,回调,参数,组件等是否在当前版本可用. ca ...

  9. vuex记录

    vuex就是vue中管理状态的地方,控制着组件之间的数据: 5大核心,通常只要有state和mutation就能满足vuex最基本的需求 1.state 项目存放各种状态的地方 2.mutation ...

  10. laravel ajax 实现省市区三级联动

    首先将省市区的数据存储到数据库中 sql文件地址:http://m.caomeipi.com/ html <select name="province" style=&quo ...