• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode795.区间子数组个数|NumberofSubarrayswithBoundedMaximum

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10547191.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

We are given an array A of positive integers, and two positive integers L and R (L <= R).

Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least L and at most R.

Example :
Input: 
A = [2, 1, 4, 3]
L = 2
R = 3
Output: 3
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].

Note:

  • L, R  and A[i] will be an integer in the range [0, 10^9].
  • The length of A will be in the range of [1, 50000].

给定一个元素都是正整数的数组A ,正整数 L 以及 R (L <= R)。

求连续、非空且其中最大元素满足大于等于L 小于等于R的子数组个数。

例如 :
输入: 
A = [2, 1, 4, 3]
L = 2
R = 3
输出: 3
解释: 满足条件的子数组: [2], [2, 1], [3].

注意:

  • L, R  和 A[i] 都是整数,范围在 [0, 10^9]
  • 数组 A 的长度范围在[1, 50000]

Runtime: 256ms

Memory Usage: 19.6 MB
 1 class Solution {
 2     func numSubarrayBoundedMax(_ A: [Int], _ L: Int, _ R: Int) -> Int {
 3         var res:Int = 0
 4         var left:Int = -1
 5         var right:Int = -1
 6         for i in 0..<A.count
 7         {
 8             if A[i] > R
 9             {
10                 left = i
11                 right = i
12                 continue
13             }
14             if A[i] >= L
15             {
16                 right = i
17             }
18             res += right - left
19         }
20         return res
21     }
22 }

Runtime: 288 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func numSubarrayBoundedMax(_ A: [Int], _ L: Int, _ R: Int) -> Int {
 3          var first = 0,second = 0
 4         var result = 0,rangeCount = 0
 5         
 6         while second < A.count && first < A.count {
 7             if A[second] >= L && A[second] <= R {
 8                 result += (second - first + 1)
 9                 rangeCount = second - first + 1
10             } else if (A[second] < L) {
11                 result += rangeCount
12             } else { // A[second] > R
13                 first = second + 1
14                 rangeCount = 0
15             }
16             second += 1
17         }
18         return result
19     }    
20 }

300ms

 1 class Solution {
 2     func numSubarrayBoundedMax(_ A: [Int], _ L: Int, _ R: Int) -> Int {
 3         var result = 0
 4         var numberOfLows = 0
 5         var numberOfEquals = 0
 6         enum Status {
 7             case equal
 8             case low
 9             case high
10         }
11         var status = Status.equal
12         
13         for (_, value) in A.enumerated() {
14             switch status {
15             case .equal:
16                 if value < L {
17                     numberOfLows = 1
18                     numberOfEquals += 1
19                 } else if value > R {
20                     result += numberOfSubarrayInLength(numberOfEquals)
21                     numberOfEquals = 0
22                 } else {
23                     numberOfEquals += 1
24                 }
25             case .low:
26                 if value < L {
27                     numberOfLows += 1
28                     numberOfEquals += 1
29                 } else if value > R {
30                     result += numberOfSubarrayInLength(numberOfEquals)
31                     numberOfEquals = 0
32                     result -= numberOfSubarrayInLength(numberOfLows)
33                     numberOfLows = 0
34                 } else {
35                     numberOfEquals += 1
36                     result -= numberOfSubarrayInLength(numberOfLows)
37                     numberOfLows = 0
38                 }
39             case .high:
40                 if value < L {
41                     numberOfLows = 1
42                     numberOfEquals = 1
43                 } else if value > R {
44                     
45                 } else {
46                     numberOfLows = 0
47                     numberOfEquals = 1
48                 }
49             }
50             if value < L {
51                 status = .low
52             } else if value > R {
53                 status = .high
54             } else {
55                 status = .equal
56             }
57         }
58         result -= numberOfSubarrayInLength(numberOfLows)
59         result += numberOfSubarrayInLength(numberOfEquals)
60         return result
61     }
62     
63     func numberOfSubarrayInLength(_ n: Int) -> Int {
64         if n <= 0 {
65             return 0
66         }
67         
68         return n * (n + 1) / 2
69     }
70 }

356ms

 1 class Solution {
 2     func numSubarrayBoundedMax(_ A: [Int], _ L: Int, _ R: Int) -> Int {
 3         var count = 0
 4         for i in 0..<A.count {
 5             var currMax = A[i]
 6             for j in i..<A.count {
 7                 currMax = max(currMax, A[j])
 8                 if currMax >= L && currMax <= R {
 9                     count += 1
10                 } else if currMax > R {
11                     break
12                 }
13             }
14         }
15         return count
16     }
17 }

3816ms

 1 class Solution {
 2     func numSubarrayBoundedMax(_ A: [Int], _ L: Int, _ R: Int) -> Int {
 3         if A.count <= 0 { return 0 }
 4         var cursor = 0
 5         var count = 0
 6         while cursor < A.count {
 7             for i in cursor ..< A.count {
 8                 let temp = A[cursor...i]
 9                 let max = temp.max()!
10                 if max > R {
11                     break
12                 }
13                 if max >= L {
14                     count += 1
15                 }
16             }
17             cursor += 1
18         }
19         return count
20     }
21 }

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swiftで作るToDoアプリ開発チュートリアル发布时间:2022-07-13
下一篇:
[Swift]LeetCode268.缺失数字|MissingNumber发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap