From 37d963ce4f75b38eb8f05550204921c84981bf54 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Fri, 13 Aug 2021 16:50:26 +0800 Subject: [PATCH] =?UTF-8?q?713:=E4=B9=98=E7=A7=AF=E5=B0=8F=E4=BA=8EK?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/SubarrayProductLessThanK.java | 75 +++++++++++++++++++ .../editor/cn/SubarrayProductLessThanK.md | 31 ++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.java create mode 100644 src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.md diff --git a/src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.java b/src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.java new file mode 100644 index 0000000..d5b0ebd --- /dev/null +++ b/src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.java @@ -0,0 +1,75 @@ +//给定一个正整数数组 nums和整数 k 。 +// +// 请找出该数组内乘积小于 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的子数组。 +// +// +// 示例 2: +// +// +//输入: nums = [1,2,3], k = 0 +//输出: 0 +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 3 * 104 +// 1 <= nums[i] <= 1000 +// 0 <= k <= 106 +// +// Related Topics 数组 滑动窗口 +// 👍 272 👎 0 + +package leetcode.editor.cn; + +//713:乘积小于K的子数组 +class SubarrayProductLessThanK { + public static void main(String[] args) { + //测试代码 + Solution solution = new SubarrayProductLessThanK().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int numSubarrayProductLessThanK(int[] nums, int k) { + if (k == 0) { + return 0; + } + double logk = Math.log(k); + double[] logs = new double[nums.length + 1]; + for (int i = 0; i < nums.length; i++) { + logs[i + 1] = logs[i] + Math.log(nums[i]); + } + + int result = 0; + for (int i = 0; i < logs.length; i++) { + int start = i + 1; + int end = logs.length; + while (start < end) { + int mid = (end + start) / 2; + if (logs[mid] < logs[i] + logk - 1e-9) { + start = mid + 1; + } else { + end = mid; + } + } + result += start - i - 1; + } + return result; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.md b/src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.md new file mode 100644 index 0000000..0ec7a1c --- /dev/null +++ b/src/main/java/leetcode/editor/cn/SubarrayProductLessThanK.md @@ -0,0 +1,31 @@ +

给定一个正整数数组 nums和整数 k

+ +

请找出该数组内乘积小于 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的子数组。
+
+ +

示例 2:

+ +
+输入: nums = [1,2,3], k = 0
+输出: 0
+ +

 

+ +

提示: 

+ + +
Related Topics
  • 数组
  • 滑动窗口
  • \n
  • 👍 272
  • 👎 0
  • \ No newline at end of file