我是靠谱客的博主 生动吐司,这篇文章主要介绍Leetcode 713. Subarray Product Less Than K,现在分享给大家,希望可以做个参考。

Leetcode 713. Subarray Product Less Than K

Input: nums = [10, 5, 2, 6], k = 100
Output: 8

index:0
[10], [10, 5], ans = 2
index: 1
[5], [5,2], [5,2,6], ans = 5
index: 2
[2], [2, 6], ans = 7
index: 3
[6], ans =8

Approach #2: Sliding Window [Accepted]

Input: nums = [10, 5, 2, 6], k = 100
Output: 8

right: 0
[10], ans =1
right: 1
[10,5], [5], ans =3
right: 2
[5,2], [2], ans = 5
right: 3
[5,2,6] ,[2,6], [6], ans =8

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution { public int numSubarrayProductLessThanK(int[] nums, int k) { if (k <= 1) return 0; int prod = 1, ans = 0, left = 0; for (int right = 0; right < nums.length; right++) { prod *= nums[right]; while (prod >= k) prod /= nums[left++]; ans += right - left + 1; System.out.println(ans); } return ans; } }

最后

以上就是生动吐司最近收集整理的关于Leetcode 713. Subarray Product Less Than K的全部内容,更多相关Leetcode内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部