敢教日月换新天。为有牺牲多壮志,

[Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

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

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

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

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

Your are given an array of positive integers nums.

Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.

Example 1:

Input: nums = [10, 5, 2, 6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.

Note:

  • 0 < nums.length <= 50000.
  • 0 < nums[i] < 1000.
  • 0 <= k < 10^6.

给定一个正整数数组 nums

找出该数组内乘积小于 k 的连续的子数组的个数。

示例 1:

输入: nums = [10,5,2,6], k = 100
输出: 8
解释: 8个乘积小于100的子数组分别为: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]。
需要注意的是 [10,5,2] 并不是乘积小于100的子数组。

说明:

  • 0 < nums.length <= 50000
  • 0 < nums[i] < 1000
  • 0 <= k < 10^6

 872ms

复制代码
 1 class Solution {
 2     func numSubarrayProductLessThanK(_ nums: [Int], _ k: Int) -> Int {
 3         guard nums.count > 0 else {
 4             return 0 
 5         }
 6         
 7         var movingIndex: Int = 0 
 8         var startIndex: Int = 0 
 9         var product: Int = 1 
10         var result: Int = 0 
11         
12         while movingIndex < nums.count {
13             let num = nums[movingIndex]
14             product *= num 
15              while product >= k && startIndex < movingIndex {
16                     product /= nums[startIndex]
17                     startIndex += 1
18             }
19             if product < k {
20                 result += movingIndex - startIndex + 1
21             }
22              movingIndex += 1           
23         }
24         
25         return result
26     }
27 }
复制代码

Runtime: 880 ms
Memory Usage: 19 MB
复制代码
 1 class Solution {
 2     func numSubarrayProductLessThanK(_ nums: [Int], _ k: Int) -> Int {
 3         if k <= 1 {return 0}
 4         var res:Int = 0
 5         var prod:Int = 1
 6         var left:Int = 0
 7         for i in 0..<nums.count
 8         {
 9             prod *= nums[i]
10             while(prod >= k)
11             {
12                 prod /= nums[left]
13                 left += 1
14             }
15             res += i - left + 1
16         }
17         return res
18     }
19 }
复制代码

896ms

复制代码
 1 class Solution {
 2     func numSubarrayProductLessThanK(_ nums: [Int], _ k: Int) -> Int {
 3 
 4     var left: Int = 0
 5     var sum = 1,totle = 0,right = 0;
 6     
 7     if k <= sum {
 8         return 0
 9     }
10     
11     for (index, value) in nums.enumerated() {
12         
13         sum *= value;
14         while sum >= k {
15             sum = sum/nums[left]
16             left += 1
17         }
18         totle += ((right - left) + 1)
19         right += 1
20         
21     }
22     return totle;
23   }
24 }
复制代码

 

 

posted @   为敢技术  阅读(315)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°