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. Rinetd linxu TCP 端口转发

    Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具,实现端口映射/转发/重定向.Rinetd是单一过程的服务器,它处理任何数量的连接到在配置文件etc/r ...

  2. Linux安装证书

    Linux安装 vCenter root CA: 1.访问vCenter管理页面,下载"下载受信任的根 CA 证书" 2.压缩文件内带有数字作为扩展名(.0..1 等)的文件是根证 ...

  3. postman或浏览器可以访问,java不能访问的post请求,连接超时

    代码中用RestTemplate请求url一直是连接超时 可以修改一下jvm配置 -Djava.net.preferIPv4Stack=true

  4. python中的链表推导式

    python中的链表推导式 博客分类: Python Python  num=[1,2,3] myvec=[[x,x*2] for x in num] #嵌套一个链表,格式为一个数和他的平方 prin ...

  5. 运行yarn报错:error C:\liuyan\tools\echarts-5.4.0\node_modules\cwebp-bin: Command failed.

    完成warning和报错信息如下. 通过报错信息提示,锁定cwebp-bin,在waring中发现有提示说要更新至7或更高版本. 解决方案:在package.json中,将cwebp-bin设置版本为 ...

  6. .net基础—委托和事件

    委托 委托是一种引用类型,表示对具有特定参数列表和返回类型的方法的引用. 在实例化委托时,可以将其实例与任何具有兼容签名和返回类型的方法相关联. 可以通过委托实例调用方法.可以将任何可访问类或结构中与 ...

  7. 此平台不支持虚拟化的 Intel VT-x/EPT。不使用虚拟化的 Intel VT-x/EPT,是否继续?

    1.cpu虚拟化是否打开 2.Windows安全中心>设备安全性>内核隔离

  8. 计数 dp 部分例题(六~十部分)

    六.转化求和顺序(線形和への分解) 例题1 题意 有一个长为 \(n\) 的数组 \(a\).求在 \(a\) 中选择 \(k\) 个数的所有方案中,每次选择的所有数的中位数的和.\(n\le 10^ ...

  9. unittest框架数据驱动

    一.目录 数据驱动概述 环境准备 使用unittest和ddt驱动 使用数据文件驱动 使用Excel驱动 使用XML驱动 使用MySQL驱动 二.数据驱动概述 数据驱动的定义: 相同的测试脚本使用不同 ...

  10. 从URL中获取参数

    1.跳转测试页面  获取的url上的参数    <!doctype html> <html lang="en"> <head>     < ...