Skip to content

Commit 45bc686

Browse files
committed
update: 添加问题“2302.统计得分小于K的子数组数目”的代码和题解(#905)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 1eae9ea commit 45bc686

7 files changed

+322
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-29 23:46:23
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-29 23:58:18
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
13+
class Solution {
14+
public:
15+
long long countSubarrays(vector<int>& nums, long long k) {
16+
vector<ll> suffix(nums.size() + 1);
17+
for (int i = 0; i < nums.size(); i++) {
18+
suffix[i + 1] = suffix[i] + nums[i];
19+
}
20+
ll ans = 0;
21+
for (int l = 0, r = 0; r < nums.size(); r++) {
22+
// while ((suffix[r + 1] - suffix[l]) * (r - l + 1) >= k && l < r) { // 不可,否则可能因为l=r退出循环,导致[i, i]大于k但仍被统计
23+
while ((suffix[r + 1] - suffix[l]) * (r - l + 1) >= k) {
24+
l++;
25+
}
26+
ans += r - l + 1;
27+
// cout << "[" << l << ", " << r << "]" << endl;
28+
}
29+
return ans;
30+
}
31+
};
32+
33+
#if defined(_WIN32) || defined(__APPLE__)
34+
/*
35+
[2,1,4,3,5]
36+
10
37+
38+
6
39+
*/
40+
/*
41+
[9,5,3,8,4,7,2,7,4,5,4,9,1,4,8,10,8,10,4,7]
42+
4
43+
44+
3
45+
*/
46+
int main() {
47+
string s;
48+
int k;
49+
while (cin >> s >> k) {
50+
vector<int> v = stringToVector(s);
51+
Solution sol;
52+
cout << sol.countSubarrays(v, k) << endl;
53+
}
54+
return 0;
55+
}
56+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-29 23:46:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-30 10:55:53
6+
*/
7+
package main
8+
9+
func countSubarrays(nums []int, k int64) (ans int64) {
10+
cnt := int64(0)
11+
l := 0
12+
for r, t := range nums {
13+
cnt += int64(t)
14+
for cnt * int64(r - l + 1) >= k {
15+
cnt -= int64(nums[l])
16+
l++
17+
}
18+
ans += int64(r - l + 1)
19+
}
20+
return
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-29 23:46:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-30 10:41:19
6+
*/
7+
class Solution {
8+
public long countSubarrays(int[] nums, long k) {
9+
long ans = 0, cnt = 0;
10+
for (int l = 0, r = 0; r < nums.length; r++) {
11+
cnt += nums[r];
12+
while (cnt * (r - l + 1) >= k) {
13+
cnt -= nums[l++];
14+
}
15+
ans += (r - l + 1);
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-29 23:46:38
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-30 10:39:13
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def countSubarrays(self, nums: List[int], k: int) -> int:
11+
ans = cnt = l = 0
12+
for r in range(len(nums)):
13+
cnt += nums[r]
14+
while cnt * (r - l + 1) >= k:
15+
cnt -= nums[l]
16+
l += 1
17+
ans += (r - l + 1)
18+
return ans
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-30 10:29:37
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-30 10:34:24
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
13+
class Solution {
14+
public:
15+
long long countSubarrays(vector<int>& nums, long long k) {
16+
ll ans = 0, cnt = 0;
17+
for (int l = 0, r = 0; r < nums.size(); r++) {
18+
cnt += nums[r];
19+
while (cnt * (r - l + 1) >= k) {
20+
cnt -= nums[l++];
21+
}
22+
ans += (r - l + 1);
23+
}
24+
return ans;
25+
}
26+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@
705705
|2296.设计一个文本编辑器|困难|<a href="https://leetcode.cn/problems/design-a-text-editor/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/27/LeetCode%202296.%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145898693" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/design-a-text-editor/solutions/3427866/letmefly-2296she-ji-yi-ge-wen-ben-bian-j-bgln/" target="_blank">LeetCode题解</a>|
706706
|2299.强密码检验器II|简单|<a href="https://leetcode.cn/problems/strong-password-checker-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/19/LeetCode%202299.%E5%BC%BA%E5%AF%86%E7%A0%81%E6%A3%80%E9%AA%8C%E5%99%A8II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128738747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/strong-password-checker-ii/solutions/2070064/letmefly-2299qiang-mi-ma-jian-yan-qi-ii-60u29/" target="_blank">LeetCode题解</a>|
707707
|2300.咒语和药水的成功对数|中等|<a href="https://leetcode.cn/problems/successful-pairs-of-spells-and-potions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/10/LeetCode%202300.%E5%92%92%E8%AF%AD%E5%92%8C%E8%8D%AF%E6%B0%B4%E7%9A%84%E6%88%90%E5%8A%9F%E5%AF%B9%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134332611" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/successful-pairs-of-spells-and-potions/solutions/2520817/letmefly-2300zhou-yu-he-yao-shui-de-chen-xi4c/" target="_blank">LeetCode题解</a>|
708+
|2302.统计得分小于K的子数组数目|困难|<a href="https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/30/LeetCode%202302.%E7%BB%9F%E8%AE%A1%E5%BE%97%E5%88%86%E5%B0%8F%E4%BA%8EK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147637062" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/solutions/3666361/letmefly-2302tong-ji-de-fen-xiao-yu-k-de-3vkc/" target="_blank">LeetCode题解</a>|
708709
|2303.计算应缴税款总额|简单|<a href="https://leetcode.cn/problems/calculate-amount-paid-in-taxes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/23/LeetCode%202303.%E8%AE%A1%E7%AE%97%E5%BA%94%E7%BC%B4%E7%A8%8E%E6%AC%BE%E6%80%BB%E9%A2%9D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128751683" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/calculate-amount-paid-in-taxes/solutions/2073612/letmefly-2303ji-suan-ying-jiao-shui-kuan-lzsi/" target="_blank">LeetCode题解</a>|
709710
|2304.网格中的最小路径代价|中等|<a href="https://leetcode.cn/problems/minimum-path-cost-in-a-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/22/LeetCode%202304.%E7%BD%91%E6%A0%BC%E4%B8%AD%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%AF%E5%BE%84%E4%BB%A3%E4%BB%B7/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134563145" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-path-cost-in-a-grid/solutions/2537837/letmefly-2304wang-ge-zhong-de-zui-xiao-l-bjad/" target="_blank">LeetCode题解</a>|
710711
|2309.兼具大小写的最好英文字母|简单|<a href="https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/27/LeetCode%202309.%E5%85%BC%E5%85%B7%E5%A4%A7%E5%B0%8F%E5%86%99%E7%9A%84%E6%9C%80%E5%A5%BD%E8%8B%B1%E6%96%87%E5%AD%97%E6%AF%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128769360" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/solutions/2077745/letmefly-2309jian-ju-da-xiao-xie-de-zui-3hgag/" target="_blank">LeetCode题解</a>|
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
---
2+
title: 2302.统计得分小于 K 的子数组数目:滑动窗口(不需要前缀和)
3+
date: 2025-04-30 17:01:00
4+
tags: [题解, LeetCode, 困难, 数组, 二分查找, 前缀和, 滑动窗口]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2302.统计得分小于 K 的子数组数目:滑动窗口(不需要前缀和)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/](https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/)
11+
12+
<p>一个数组的 <strong>分数</strong>&nbsp;定义为数组之和 <strong>乘以</strong>&nbsp;数组的长度。</p>
13+
14+
<ul>
15+
<li>比方说,<code>[1, 2, 3, 4, 5]</code>&nbsp;的分数为&nbsp;<code>(1 + 2 + 3 + 4 + 5) * 5 = 75</code>&nbsp;。</li>
16+
</ul>
17+
18+
<p>给你一个正整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;,请你返回&nbsp;<code>nums</code>&nbsp;中分数&nbsp;<strong>严格小于&nbsp;</strong><code>k</code>&nbsp;&nbsp;<strong>非空整数子数组数目</strong>。</p>
19+
20+
<p><strong>子数组</strong> 是数组中的一个连续元素序列。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong>示例 1:</strong></p>
25+
26+
<pre>
27+
<b>输入:</b>nums = [2,1,4,3,5], k = 10
28+
<b>输出:</b>6
29+
<strong>解释:</strong>
30+
有 6 个子数组的分数小于 10 :
31+
- [2] 分数为 2 * 1 = 2 。
32+
- [1] 分数为 1 * 1 = 1 。
33+
- [4] 分数为 4 * 1 = 4 。
34+
- [3] 分数为 3 * 1 = 3 。
35+
- [5] 分数为 5 * 1 = 5 。
36+
- [2,1] 分数为 (2 + 1) * 2 = 6 。
37+
注意,子数组 [1,4] 和 [4,3,5] 不符合要求,因为它们的分数分别为 10 和 36,但我们要求子数组的分数严格小于 10 。</pre>
38+
39+
<p><strong>示例 2:</strong></p>
40+
41+
<pre>
42+
<b>输入:</b>nums = [1,1,1], k = 5
43+
<b>输出:</b>5
44+
<strong>解释:</strong>
45+
除了 [1,1,1] 以外每个子数组分数都小于 5 。
46+
[1,1,1] 分数为 (1 + 1 + 1) * 3 = 9 ,大于 5 。
47+
所以总共有 5 个子数组得分小于 5 。
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
52+
<p><strong>提示:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
56+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
57+
<li><code>1 &lt;= k &lt;= 10<sup>15</sup></code></li>
58+
</ul>
59+
60+
Hard里面的简单题。
61+
62+
## 解题方法:滑动窗口
63+
64+
本题计算的是字数组和乘以数组长度,由于数组元素为正,所以数组中元素越多计算得到的值越大,**具有单调性**,可以使用滑动窗口。
65+
66+
使用$cnt$记录窗口中元素总和,右指针依次遍历数组中每个元素作为窗口终点(遍历时将指向元素加入窗口)。
67+
68+
对于每个右指针的位置:
69+
70+
> 如果$cnt*len(subArray)\geq k$,则不断右移左指针。
71+
>
72+
> 左指针移动结束后所在位置就是第一个窗口“分数”小于“k”的位置,从左指针到右指针任一元素**开始**到右指针所指元素**结束**的子数组“分数”都小于“k”,都是合法子数组。总个数为$r-l+1$,累加到答案中即可。
73+
74+
+ 时间复杂度$O(len(nums))$
75+
+ 空间复杂度$O(1)$
76+
77+
### AC代码
78+
79+
#### C++
80+
81+
```cpp
82+
/*
83+
* @Author: LetMeFly
84+
* @Date: 2025-04-30 10:29:37
85+
* @LastEditors: LetMeFly.xyz
86+
* @LastEditTime: 2025-04-30 10:34:24
87+
*/
88+
typedef long long ll;
89+
90+
class Solution {
91+
public:
92+
long long countSubarrays(vector<int>& nums, long long k) {
93+
ll ans = 0, cnt = 0;
94+
for (int l = 0, r = 0; r < nums.size(); r++) {
95+
cnt += nums[r];
96+
while (cnt * (r - l + 1) >= k) {
97+
cnt -= nums[l++];
98+
}
99+
ans += (r - l + 1);
100+
}
101+
return ans;
102+
}
103+
};
104+
```
105+
106+
#### Python
107+
108+
```python
109+
'''
110+
Author: LetMeFly
111+
Date: 2025-04-29 23:46:38
112+
LastEditors: LetMeFly.xyz
113+
LastEditTime: 2025-04-30 10:39:13
114+
'''
115+
from typing import List
116+
117+
class Solution:
118+
def countSubarrays(self, nums: List[int], k: int) -> int:
119+
ans = cnt = l = 0
120+
for r in range(len(nums)):
121+
cnt += nums[r]
122+
while cnt * (r - l + 1) >= k:
123+
cnt -= nums[l]
124+
l += 1
125+
ans += (r - l + 1)
126+
return ans
127+
```
128+
129+
#### Java
130+
131+
```java
132+
/*
133+
* @Author: LetMeFly
134+
* @Date: 2025-04-29 23:46:43
135+
* @LastEditors: LetMeFly.xyz
136+
* @LastEditTime: 2025-04-30 10:41:19
137+
*/
138+
class Solution {
139+
public long countSubarrays(int[] nums, long k) {
140+
long ans = 0, cnt = 0;
141+
for (int l = 0, r = 0; r < nums.length; r++) {
142+
cnt += nums[r];
143+
while (cnt * (r - l + 1) >= k) {
144+
cnt -= nums[l++];
145+
}
146+
ans += (r - l + 1);
147+
}
148+
return ans;
149+
}
150+
}
151+
```
152+
153+
#### Go
154+
155+
```go
156+
/*
157+
* @Author: LetMeFly
158+
* @Date: 2025-04-29 23:46:45
159+
* @LastEditors: LetMeFly.xyz
160+
* @LastEditTime: 2025-04-30 10:55:53
161+
*/
162+
package main
163+
164+
func countSubarrays(nums []int, k int64) (ans int64) {
165+
cnt := int64(0)
166+
l := 0
167+
for r, t := range nums {
168+
cnt += int64(t)
169+
for cnt * int64(r - l + 1) >= k {
170+
cnt -= int64(nums[l])
171+
l++
172+
}
173+
ans += int64(r - l + 1)
174+
}
175+
return
176+
}
177+
```
178+
179+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147637062)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/30/LeetCode%202302.%E7%BB%9F%E8%AE%A1%E5%BE%97%E5%88%86%E5%B0%8F%E4%BA%8EK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/)哦~
180+
>
181+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)