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

[Swift]LeetCode713.乘积小于K的子数组|SubarrayProductLessThanK

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 }

 

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Swift - Spritekit 实现游戏摇杆发布时间:2022-07-13
下一篇:
[Swift]LeetCode219.存在重复元素II|ContainsDuplicateII发布时间: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