Skip to content

Commit 422bf54

Browse files
committed
+ problem 2302
1 parent 691eb7c commit 422bf54

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 2302. Count Subarrays With Score Less Than K
2+
The **score** of an array is defined as the **product** of its sum and its length.
3+
4+
* For example, the score of `[1, 2, 3, 4, 5]` is `(1 + 2 + 3 + 4 + 5) * 5 = 75`.
5+
6+
Given a positive integer array `nums` and an integer `k`, return *the **number of non-empty subarrays** of* `nums` *whose score is **strictly less** than* `k`.
7+
8+
A **subarray** is a contiguous sequence of elements within an array.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> nums = [2,1,4,3,5], k = 10
13+
<strong>Output:</strong> 6
14+
<strong>Explanation:</strong>
15+
The 6 subarrays having scores less than 10 are:
16+
- [2] with score 2 * 1 = 2.
17+
- [1] with score 1 * 1 = 1.
18+
- [4] with score 4 * 1 = 4.
19+
- [3] with score 3 * 1 = 3.
20+
- [5] with score 5 * 1 = 5.
21+
- [2,1] with score (2 + 1) * 2 = 6.
22+
Note that subarrays such as [1,4] and [4,3,5] are not considered because their scores are 10 and 36 respectively, while we need scores strictly less than 10.
23+
</pre>
24+
25+
#### Example 2:
26+
<pre>
27+
<strong>Input:</strong> nums = [1,1,1], k = 5
28+
<strong>Output:</strong> 5
29+
<strong>Explanation:</strong>
30+
Every subarray except [1,1,1] has a score less than 5.
31+
[1,1,1] has a score (1 + 1 + 1) * 3 = 9, which is greater than 5.
32+
Thus, there are 5 subarrays having scores less than 5.
33+
</pre>
34+
35+
#### Constraints:
36+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
37+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
38+
* <code>1 <= k <= 10<sup>15</sup></code>
39+
40+
## Solutions (Rust)
41+
42+
### 1. Solution
43+
```Rust
44+
impl Solution {
45+
pub fn count_subarrays(nums: Vec<i32>, k: i64) -> i64 {
46+
let mut prefix_sum = vec![0; nums.len() + 1];
47+
let mut ret = 0;
48+
49+
for i in 0..nums.len() {
50+
prefix_sum[i + 1] = prefix_sum[i] + nums[i] as i64;
51+
}
52+
53+
for i in 0..nums.len() {
54+
let mut lo = 0;
55+
let mut hi = i + 1;
56+
57+
while lo < hi {
58+
let mid = (lo + hi) / 2;
59+
let score = (prefix_sum[i + 1] - prefix_sum[mid]) * (i + 1 - mid) as i64;
60+
61+
if score < k {
62+
hi = mid;
63+
} else {
64+
lo = mid + 1;
65+
}
66+
}
67+
68+
ret += (i + 1 - hi) as i64;
69+
}
70+
71+
ret
72+
}
73+
}
74+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 2302. 统计得分小于 K 的子数组数目
2+
一个数组的 **分数** 定义为数组之和 **乘以** 数组的长度。
3+
4+
* 比方说,`[1, 2, 3, 4, 5]` 的分数为 `(1 + 2 + 3 + 4 + 5) * 5 = 75`
5+
6+
给你一个正整数数组 `nums` 和一个整数 `k` ,请你返回 `nums` 中分数 **严格小于** `k`**非空整数子数组数目**
7+
8+
**子数组** 是数组中的一个连续元素序列。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> nums = [2,1,4,3,5], k = 10
13+
<strong>输出:</strong> 6
14+
<strong>解释:</strong>
15+
有 6 个子数组的分数小于 10 :
16+
- [2] 分数为 2 * 1 = 2 。
17+
- [1] 分数为 1 * 1 = 1 。
18+
- [4] 分数为 4 * 1 = 4 。
19+
- [3] 分数为 3 * 1 = 3 。
20+
- [5] 分数为 5 * 1 = 5 。
21+
- [2,1] 分数为 (2 + 1) * 2 = 6 。
22+
注意,子数组 [1,4] 和 [4,3,5] 不符合要求,因为它们的分数分别为 10 和 36,但我们要求子数组的分数严格小于 10 。
23+
</pre>
24+
25+
#### 示例 2:
26+
<pre>
27+
<strong>输入:</strong> nums = [1,1,1], k = 5
28+
<strong>输出:</strong> 5
29+
<strong>解释:</strong>
30+
除了 [1,1,1] 以外每个子数组分数都小于 5 。
31+
[1,1,1] 分数为 (1 + 1 + 1) * 3 = 9 ,大于 5 。
32+
所以总共有 5 个子数组得分小于 5 。
33+
</pre>
34+
35+
#### 提示:
36+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
37+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
38+
* <code>1 <= k <= 10<sup>15</sup></code>
39+
40+
## 题解 (Rust)
41+
42+
### 1. 题解
43+
```Rust
44+
impl Solution {
45+
pub fn count_subarrays(nums: Vec<i32>, k: i64) -> i64 {
46+
let mut prefix_sum = vec![0; nums.len() + 1];
47+
let mut ret = 0;
48+
49+
for i in 0..nums.len() {
50+
prefix_sum[i + 1] = prefix_sum[i] + nums[i] as i64;
51+
}
52+
53+
for i in 0..nums.len() {
54+
let mut lo = 0;
55+
let mut hi = i + 1;
56+
57+
while lo < hi {
58+
let mid = (lo + hi) / 2;
59+
let score = (prefix_sum[i + 1] - prefix_sum[mid]) * (i + 1 - mid) as i64;
60+
61+
if score < k {
62+
hi = mid;
63+
} else {
64+
lo = mid + 1;
65+
}
66+
}
67+
68+
ret += (i + 1 - hi) as i64;
69+
}
70+
71+
ret
72+
}
73+
}
74+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn count_subarrays(nums: Vec<i32>, k: i64) -> i64 {
3+
let mut prefix_sum = vec![0; nums.len() + 1];
4+
let mut ret = 0;
5+
6+
for i in 0..nums.len() {
7+
prefix_sum[i + 1] = prefix_sum[i] + nums[i] as i64;
8+
}
9+
10+
for i in 0..nums.len() {
11+
let mut lo = 0;
12+
let mut hi = i + 1;
13+
14+
while lo < hi {
15+
let mid = (lo + hi) / 2;
16+
let score = (prefix_sum[i + 1] - prefix_sum[mid]) * (i + 1 - mid) as i64;
17+
18+
if score < k {
19+
hi = mid;
20+
} else {
21+
lo = mid + 1;
22+
}
23+
}
24+
25+
ret += (i + 1 - hi) as i64;
26+
}
27+
28+
ret
29+
}
30+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,7 @@
14891489
[2299][2299l]|[Strong Password Checker II][2299] |![rs]
14901490
[2300][2300l]|[Successful Pairs of Spells and Potions][2300] |![py]
14911491
[2301][2301l]|[Match Substring After Replacement][2301] |![rs]
1492+
[2302][2302l]|[Count Subarrays With Score Less Than K][2302] |![rs]
14921493
[2303][2303l]|[Calculate Amount Paid in Taxes][2303] |![rs]
14931494
[2304][2304l]|[Minimum Path Cost in a Grid][2304] |![rs]
14941495
[2306][2306l]|[Naming a Company][2306] |![py]
@@ -3169,6 +3170,7 @@
31693170
[2299]:Problemset/2299-Strong%20Password%20Checker%20II/README.md#2299-strong-password-checker-ii
31703171
[2300]:Problemset/2300-Successful%20Pairs%20of%20Spells%20and%20Potions/README.md#2300-successful-pairs-of-spells-and-potions
31713172
[2301]:Problemset/2301-Match%20Substring%20After%20Replacement/README.md#2301-match-substring-after-replacement
3173+
[2302]:Problemset/2302-Count%20Subarrays%20With%20Score%20Less%20Than%20K/README.md#2302-count-subarrays-with-score-less-than-k
31723174
[2303]:Problemset/2303-Calculate%20Amount%20Paid%20in%20Taxes/README.md#2303-calculate-amount-paid-in-taxes
31733175
[2304]:Problemset/2304-Minimum%20Path%20Cost%20in%20a%20Grid/README.md#2304-minimum-path-cost-in-a-grid
31743176
[2306]:Problemset/2306-Naming%20a%20Company/README.md#2306-naming-a-company
@@ -4843,6 +4845,7 @@
48434845
[2299l]:https://leetcode.com/problems/strong-password-checker-ii/
48444846
[2300l]:https://leetcode.com/problems/successful-pairs-of-spells-and-potions/
48454847
[2301l]:https://leetcode.com/problems/match-substring-after-replacement/
4848+
[2302l]:https://leetcode.com/problems/count-subarrays-with-score-less-than-k/
48464849
[2303l]:https://leetcode.com/problems/calculate-amount-paid-in-taxes/
48474850
[2304l]:https://leetcode.com/problems/minimum-path-cost-in-a-grid/
48484851
[2306l]:https://leetcode.com/problems/naming-a-company/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,7 @@
14891489
[2299][2299l]|[强密码检验器 II][2299] |![rs]
14901490
[2300][2300l]|[咒语和药水的成功对数][2300] |![py]
14911491
[2301][2301l]|[替换字符后匹配][2301] |![rs]
1492+
[2302][2302l]|[统计得分小于 K 的子数组数目][2302] |![rs]
14921493
[2303][2303l]|[计算应缴税款总额][2303] |![rs]
14931494
[2304][2304l]|[网格中的最小路径代价][2304] |![rs]
14941495
[2306][2306l]|[公司命名][2306] |![py]
@@ -3169,6 +3170,7 @@
31693170
[2299]:Problemset/2299-Strong%20Password%20Checker%20II/README_CN.md#2299-强密码检验器-ii
31703171
[2300]:Problemset/2300-Successful%20Pairs%20of%20Spells%20and%20Potions/README_CN.md#2300-咒语和药水的成功对数
31713172
[2301]:Problemset/2301-Match%20Substring%20After%20Replacement/README_CN.md#2301-替换字符后匹配
3173+
[2302]:Problemset/2302-Count%20Subarrays%20With%20Score%20Less%20Than%20K/README_CN.md#2302-统计得分小于-k-的子数组数目
31723174
[2303]:Problemset/2303-Calculate%20Amount%20Paid%20in%20Taxes/README_CN.md#2303-计算应缴税款总额
31733175
[2304]:Problemset/2304-Minimum%20Path%20Cost%20in%20a%20Grid/README_CN.md#2304-网格中的最小路径代价
31743176
[2306]:Problemset/2306-Naming%20a%20Company/README_CN.md#2306-公司命名
@@ -4843,6 +4845,7 @@
48434845
[2299l]:https://leetcode.cn/problems/strong-password-checker-ii/
48444846
[2300l]:https://leetcode.cn/problems/successful-pairs-of-spells-and-potions/
48454847
[2301l]:https://leetcode.cn/problems/match-substring-after-replacement/
4848+
[2302l]:https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/
48464849
[2303l]:https://leetcode.cn/problems/calculate-amount-paid-in-taxes/
48474850
[2304l]:https://leetcode.cn/problems/minimum-path-cost-in-a-grid/
48484851
[2306l]:https://leetcode.cn/problems/naming-a-company/

0 commit comments

Comments
 (0)