[Swift]LeetCode845. 数组中的最长山脉 | Longest Mountain in Array

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

  • B.length >= 3
  • There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]

(Note that B could be any subarray of A, including the entire array A.)

Given an array A of integers, return the length of the longest mountain.

Return 0 if there is no mountain.

Example 1:

Input: [2,1,4,7,3,2,5]
Output: 5
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.

Example 2:

Input: [2,2,2]
Output: 0
Explanation: There is no mountain.

Note:

  1. 0 <= A.length <= 10000
  2. 0 <= A[i] <= 10000

Follow up:

  • Can you solve it using only one pass?
  • Can you solve it in O(1) space?


我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”:

  • B.length >= 3
  • 存在 0 < i < B.length - 1 使得 B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]

(注意:B 可以是 A 的任意子数组,包括整个数组 A。)

给出一个整数数组 A,返回最长 “山脉” 的长度。

如果不含有 “山脉” 则返回 0

示例 1:

输入:[2,1,4,7,3,2,5]
输出:5
解释:最长的 “山脉” 是 [1,4,7,3,2],长度为 5。

示例 2:

输入:[2,2,2]
输出:0
解释:不含 “山脉”。 

提示:

  1. 0 <= A.length <= 10000
  2. 0 <= A[i] <= 10000


156ms

 1 class Solution {
 2     func longestMountain(_ A: [Int]) -> Int {
 3         var longest = 0
 4         var increasing = true
 5         var mountainLength = 0
 6         var hasReturn = false
 7         var i = 0
 8         while  i < A.count - 1{
 9             if increasing {
10                 if A[i] < A[i+1] {
11                     i += 1
12                     mountainLength += 1
13                 }else if A[i] > A[i+1] {
14                     increasing = false
15                 }else {
16                     i += 1
17                     mountainLength = 0
18                 }
19             }else {
20                 if mountainLength == 0 {
21                      if A[i] >= A[i+1] {
22                         i += 1
23                     }else {
24                          increasing = true
25                     }
26                 }else {
27                     if A[i] > A[i+1] {
28                         hasReturn = true
29                         i += 1
30                         mountainLength += 1
31                     }else {
32                         longest = max(longest, mountainLength + 1)
33                         mountainLength  = 0
34                         increasing = false
35                     }
36                 }
37             }
38         }
39         if hasReturn {
40             return max(longest, mountainLength + 1)
41         }else {
42             return longest
43         }
44     }
45 }


Runtime: 164 ms

Memory Usage: 19.3 MB

 1 class Solution {
 2     func longestMountain(_ A: [Int]) -> Int {
 3         var N:Int = A.count
 4         var res:Int = 0
 5         var up:[Int] = [Int](repeating:0,count:N)
 6         var down:[Int] = [Int](repeating:0,count:N)
 7         for i in stride(from:N - 2,through:0,by:-1)
 8         {
 9             if A[i] > A[i + 1]
10             {
11                 down[i] = down[i + 1] + 1
12             }
13         }
14         for i in 0..<N
15         {
16             if i > 0 && A[i] > A[i - 1]
17             {
18                 up[i] = up[i - 1] + 1
19             }
20             if up[i] > 0 && down[i] > 0
21             {
22                 res = max(res, up[i] + down[i] + 1)
23             }
24         }
25         return res
26     }
27 }


168ms

 1 class Solution {
 2     func longestMountain(_ A: [Int]) -> Int {
 3         if (A.count < 3) {
 4             return 0
 5         }
 6         var l2r = Array(repeating: 0, count: A.count)
 7         var r2l = Array(repeating: 0, count: A.count)
 8
 9         for i in 1..<A.count {
10             if A[i] > A[i-1] {
11                 l2r[i] = l2r[i-1] + 1
12             }
13         }
14
15         for i in stride(from: A.count - 2, through: 1, by: -1) {
16             if A[i] > A[i+1] {
17                 r2l[i] = r2l[i+1] + 1
18             }
19         }
20
21         var result = 0
22         for i in 1..<A.count {
23             if (l2r[i] > 0 && r2l[i] > 0) {
24                 result = max(result, l2r[i] + r2l[i] + 1)
25             }
26         }
27
28         return result
29     }
30 }

原文地址:https://www.cnblogs.com/strengthen/p/10590282.html

时间: 2024-08-29 02:23:19

[Swift]LeetCode845. 数组中的最长山脉 | Longest Mountain in Array的相关文章

Longest Mountain in Array 数组中的最长山脉

我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1](注意:B 可以是 A 的任意子数组,包括整个数组 A.) 给出一个整数数组 A,返回最长 “山脉” 的长度. 如果不含有 “山脉” 则返回 0. 示例 1: 输入:[2,1,

3.分治法研究-搜索数组中的最长连续递增子集

//分治算法研究 搜索数组中的最长连续递增子集var cc=consolefunction find_max_crossing_lenarray(A,low,mid,high){    var max_left=mid,max_right=mid    var left_sum=1    var sum=0    for(var i=mid;i>low;i--){        sum=A[i]-A[i-1]        if(sum==1){            left_sum++   

[Swift]LeetCode153. 寻找旋转排序数组中的最小值 | Find Minimum in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5

[Swift]LeetCode421. 数组中两个数的最大异或值 | Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum resul

LeetCode -- 求字符串数组中的最长公共前缀

题目描写叙述: Write a function to find the longest common prefix string amongst an array of strings.就是给定1个字符串数组,找出公共最长前缀. 思路非常直接.使用1个索引来存最长公共前缀的长度就能够了. 注意, 假设使用1个字符串变量来存前缀的话,是不能AC的,由于题目不同意使用额外的空间. public string LongestCommonPrefix(string[] strs) { if(strs

[Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k. Exa

面试题:编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 &quot;&quot;。(c++实现)

实例说明 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀. 说明: 所有输入只包含小写字母 a-z . 实现方法: #include<iostream> #include<vec

[Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed). E

[Swift]LeetCode442. 数组中重复的数据 | Find All Duplicates in an Array

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,