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

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 }

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

时间: 2024-08-30 05:13:26

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

[Swift Weekly Contest 118]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2

leetcode-713 乘积小于k的数组

leetcode-713 乘积小于k的数组 参考:负雪明烛 题目描述: 给定一个正整数数组 nums.找出该数组内乘积小于 k 的连续的子数组的个数. 注:这题和209题比较类似,但是在while判断的时候需要考虑一下 class Solution(object): def numSubarrayProductLessThanK(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int "&

Leetcode 713 Subarray Product Less Than K (子数组乘积大于K的个数) (双指针)

目录 问题描述 例子 方法 Leetcode 713 问题描述 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

[LeetCode]560. 和为K的子数组(前缀和)

题目 给定一个整数数组和一个整数?k,你需要找到该数组中和为?k?的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为两种不同的情况. 说明 : 数组的长度为 [1, 20,000]. 数组中元素的范围是 [-1000, 1000] ,且整数?k?的范围是?[-1e7, 1e7]. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/subarray-sum-eq

[Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M.  (For clarification, the L-length subarray could occur before or after the M-length subarray.) Fo

LeetCode 713. Subarray Product Less Than K

Problem Description: 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. 题解: 很快想到了two pointer的解法,但是一直被corner case困住. 两个典型的case [

和为K的子数组-哈希表

class Solution { public int subarraySum(int[] nums, int k) { HashMap<Integer,Integer> h=new HashMap<>(); int sum=0; int count=0; h.put(0,1); for(int i=0;i<nums.length;i++){ sum+=nums[i]; if(h.containsKey(sum-k)){ count+=h.get(sum-k); } h.pu

713. 乘积小于K的子数组

给定一个正整数数组 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 <

[Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there is no non-empty subarray with sum at least K, return -1. Example 1: Input: A = [1], K = 1 Output: 1 Example 2: Input: A = [1,2], K = 4 Output: -1 Exa