zoukankan      html  css  js  c++  java
  • [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 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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 }
  • 相关阅读:
    tuple 元组及字典dict
    day 49 css属性补充浮动 属性定位 抽屉作业
    day48 选择器(基本、层级 、属性) css属性
    day47 列表 表单 css初识
    day 46 http和html
    day 45索引
    day 44 练习题讲解 多表查询
    day 40 多表查询 子查询
    day39 表之间的关联关系、 补充 表操作总结 where 、group by、
    day38 数据类型 约束条件
  • 原文地址:https://www.cnblogs.com/strengthen/p/10506745.html
Copyright © 2011-2022 走看看