diff --git a/content/posts/leetcode.md b/content/posts/leetcode.md index 7ae78fd..f8b2197 100644 --- a/content/posts/leetcode.md +++ b/content/posts/leetcode.md @@ -2114,3 +2114,75 @@ func abs(num int) int { ### 总结 查看最快速代码, 其将负数直接修改为整数类型最大值, 对于超过数组长度的数直接忽略, 不作处理, 其余的当作下标取对应位置的相反数. 这样处理起来思路比较清晰. + +## day29 2024-03-27 + +### 13. Subarray Product Less Than K + +Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k. + +![0327Kf6RehBJGKTs](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0327Kf6RehBJGKTs.png) + +### 题解 + +本题要求返回的是连续的相邻子数组, 第一个想到的就是滑动窗口, 设置窗口的前后指针, 当窗口内的积= k { + p /= nums[j] + j++ + } + res += i -j + 1 + } + + return res +} + +``` + +想到这, 忽然明白, 其实核心在于只需要考虑以某个位置为结尾的向前连续符合要求的数组长度作为该位置处应该增加的计数数目即可. 这样就把整个问题拆分成了独立不关联的小问题. 将每个位置应该增加的计数数目累积, 就是最终的结果.