[Swift]LeetCode896. 单调数列 | Monotonic Array

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array A is monotone increasing if for all i <= jA[i] <= A[j].  An array A is monotone decreasing if for all i <= jA[i] >= A[j].

Return true if and only if the given array A is monotonic.

Example 1:

Input: [1,2,2,3]
Output: true

Example 2:

Input: [6,5,4,4]
Output: true

Example 3:

Input: [1,3,2]
Output: false

Example 4:

Input: [1,2,4,5]
Output: true

Example 5:

Input: [1,1,1]
Output: true 

Note:

  1. 1 <= A.length <= 50000
  2. -100000 <= A[i] <= 100000


如果数组是单调递增或单调递减的,那么它是单调的

如果对于所有 i <= jA[i] <= A[j],那么数组 A 是单调递增的。 如果对于所有 i <= jA[i]> = A[j],那么数组 A 是单调递减的。

当给定的数组 A 是单调数组时返回 true,否则返回 false

示例 1:

输入:[1,2,2,3]
输出:true

示例 2:

输入:[6,5,4,4]
输出:true

示例 3:

输入:[1,3,2]
输出:false

示例 4:

输入:[1,2,4,5]
输出:true

示例 5:

输入:[1,1,1]
输出:true 

提示:

  1. 1 <= A.length <= 50000
  2. -100000 <= A[i] <= 100000

500ms

 1 class Solution {
 2     enum Order: UInt8 {
 3         case increasing
 4         case decreasing
 5         case identical
 6         case nonMonotonic
 7     }
 8
 9     func isMonotonic(_ A: [Int]) -> Bool {
10         guard A.count > 1 else {
11             return true
12         }
13
14         var last = A[0]
15         var order: Order = .identical
16
17         for idx in 1..<A.count {
18             let num = A[idx]
19             if num == last {
20                 continue
21             } else if (num < last && order == .increasing)
22             || (num > last && order == .decreasing) {
23                 order = .nonMonotonic
24                 break
25             } else if order == .identical {
26                 order = num < last ? .decreasing : .increasing
27             }
28             last = num
29         }
30         return order != .nonMonotonic
31     }
32 }


Runtime: 504 ms

Memory Usage: 19.6 MB

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var crement = true
 4         if A.last! < A.first! {
 5             crement = false
 6         }
 7         for i in 1..<A.count {
 8             if crement {
 9                 if A[i] < A[i-1] {
10                     return false
11                 }
12             } else {
13                 if A[i] > A[i-1] {
14                     return false
15                 }
16             }
17         }
18         return true
19     }
20 }


580ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         let count = A.count
 4         guard count > 2 else {
 5             return true
 6         }
 7
 8         var flag: Bool?
 9         for i in 1..<count {
10             if A[i] > A[i - 1] {
11                 if let mark = flag {
12                     if !mark {
13                         return false
14                     }
15                 } else {
16                     flag = true
17                 }
18             } else if A[i] < A[i - 1] {
19                 if let mark = flag {
20                     if mark {
21                         return false
22                     }
23                 } else {
24                     flag = false
25                 }
26             }
27         }
28         return true
29     }
30 }


588ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var store: Int = 0
 4         for i in 0..<(A.count-1){
 5             let flag = compare(A[i] , A[i+1])
 6
 7             if flag != 0{
 8                 if flag != store , store != 0{
 9                     return false
10                 }
11                 store = flag
12             }
13         }
14         return true
15     }
16
17     func compare(_ lhs: Int, _ rhs: Int) -> Int{
18         if lhs == rhs {
19             return 0
20         }
21         else if lhs > rhs{
22             return 1
23         }
24         else{
25             return -1
26         }
27     }
28 }


588ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3       if isIncreasing(A: A) || isDecreasing(A: A) {
 4           return true
 5       }
 6         return false
 7     }
 8
 9     func isIncreasing(A: [Int]) -> Bool {
10         for i in 0..<A.count-1 {
11             if A[i] > A[i+1] {
12                 return false
13             }
14         }
15         return true
16     }
17
18     func isDecreasing(A: [Int]) -> Bool {
19         for i in 0..<A.count-1 {
20             if A[i] < A[i+1] {
21                 return false
22             }
23         }
24         return true
25     }
26 }


590ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var isIncrease = true
 4         var isDecrease = true
 5         for i in 1..<A.count {
 6             if A[i-1] > A[i] {
 7                 isIncrease = false
 8                 break
 9             }
10         }
11         for i in 1..<A.count {
12             if A[i-1] < A[i] {
13                 isDecrease = false
14                 break
15             }
16         }
17         return isIncrease || isDecrease
18
19     }
20 }


608ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var up = true
 4         var down = true
 5
 6         for i in 1..<A.count {
 7             if !up || !down { }
 8             up = up && A[i - 1] <= A[i]
 9             down = down && A[i - 1] >= A[i]
10         }
11
12         return up || down
13     }
14 }


916ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var flag = 0
 4         for i in 0..<A.count-1 {
 5             if A[i+1] - A[i] != 0 {
 6                 let c = A[i+1] - A[i]
 7                 if flag * c >= 0 {
 8                     flag = c
 9                 } else {
10                     return false
11                 }
12             }
13         }
14         return true
15     }
16 }

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

时间: 2024-08-30 13:21:49

[Swift]LeetCode896. 单调数列 | Monotonic Array的相关文章

swift基本用法-数组array

数组简单用法 //------------------------------------------------------------------------------ // 1. 数组定义 // 1> 使用[]可以快速定义数组,每一个数组元素使用 , 分隔 // 2> 数组中的数据元素可以是不同类型 var array = ["hello", "swift", 1, 1.2] //---------------------------------

896. 单调数列

如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j,A[i]> = A[j],那么数组 A 是单调递减的. 当给定的数组 A 是单调数组时返回 true,否则返回 false. 示例 1: 输入:[1,2,2,3] 输出:true 示例 2: 输入:[6,5,4,4] 输出:true 示例 3: 输入:[1,3,2] 输出:false 示例 4: 输入:[1,2,4,5]

[CSP-S模拟测试]:小P的单调数列(树状数组+DP)

题目描述 小$P$最近喜欢上了单调数列,他觉得单调的数列具有非常多优美的性质.经过小$P$复杂的数学推导,他计算出了一个单调增数列的艺术价值等于该数列中所有书的总和.并且以这个为基础,小$P$还可以求出任意一个数列的艺术价值,它等于将这个数列顺次划分若干个极长单调区间(相邻两个单调区间的单调性必须不相同)后,每个单调区间中元素总和的平均值.比如对于数列$3\ 7\ 9\ 2\ 4\ 5$,它将被划分为$[3\ 7\ 9]\ [2]\ [4\ 5]$,其艺术价值为$\frac{19+2+9}{3}

[LeetCode] 896. Monotonic Array 单调数组

An array is?monotonic?if it is either monotone increasing or monotone decreasing. An array?A?is monotone increasing if for all?i <= j,?A[i] <= A[j].? An array?A?is monotone decreasing if for all?i <= j,?A[i] >= A[j]. Return?true?if and only if

896. Monotonic Array - Easy

An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if

bzoj 1012 维护一个单调数列

Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. 插入操作.语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾.限制:n是非负整数并且在长整范围内.注意:初始时数列是空的,没有一个数. Input 第一行两个整数,M和D,其中M表示操作

896. Monotonic Array

An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if

leetcode 665. 非递减数列(Non-decreasing Array)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n),满足 array[i] <= array[i + 1]. 示例 1: 输入: [4,2,3] 输出: True 解释: 你可以通过把第一个4变成1来使得它成为一个非递减数列. 示例 2: 输入: [4,2,1] 输出: False 解释:

[Swift]LeetCode561. 数组拆分 I | Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explan