You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0900-0999/0907.Sum of Subarray Minimums/README_EN.md
+31-58
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,37 @@ Sum is 17.
37
37
38
38
## Solutions
39
39
40
-
### Solution 1
40
+
### Solution 1: Monotonic Stack
41
+
42
+
The problem asks for the sum of the minimum values of each subarray, which is equivalent to finding the number of subarrays for which each element $arr[i]$ is the minimum, then multiplying by $arr[i]$, and finally summing these up.
43
+
44
+
Therefore, the focus of the problem is to find the number of subarrays for which $arr[i]$ is the minimum. For $arr[i]$, we find the first position $left[i]$ to its left that is less than $arr[i]$, and the first position $right[i]$ to its right that is less than or equal to $arr[i]$. The number of subarrays for which $arr[i]$ is the minimum is $(i - left[i]) \times (right[i] - i)$.
45
+
46
+
Note, why do we find the first position $right[i]$ to the right that is less than or equal to $arr[i]$, rather than less than $arr[i]$? This is because if we find the first position $right[i]$ to the right that is less than $arr[i]$, it will lead to duplicate calculations.
47
+
48
+
Let's take an example to illustrate. For the following array:
49
+
50
+
The element at index $3$ is $2$, the first element to its left that is less than $2$ is at index $0$. If we find the first element to its right that is less than $2$, we get index $7$. That is, the subarray interval is $(0, 7)$. Note that this is an open interval.
51
+
52
+
```
53
+
0 4 3 2 5 3 2 1
54
+
* ^ *
55
+
```
56
+
57
+
In the same way, we can find the subarray interval for the element at index $6$, and find that its subarray interval is also $(0, 7)$. That is, the subarray intervals for the elements at index $3$ and index $6$ are the same. This leads to duplicate calculations.
58
+
59
+
```
60
+
0 4 3 2 5 3 2 1
61
+
* ^ *
62
+
```
63
+
64
+
If we find the first element to its right that is less than or equal to its value, there will be no duplication, because the subarray interval for the element at index $3$ becomes $(0, 6)$, and the subarray interval for the element at index $6$ is $(0, 7)$, which are not the same.
65
+
66
+
Back to this problem, we just need to traverse the array, for each element $arr[i]$, use a monotonic stack to find the first position $left[i]$ to its left that is less than $arr[i]$, and the first position $right[i]$ to its right that is less than or equal to $arr[i]$. The number of subarrays for which $arr[i]$ is the minimum is $(i - left[i]) \times (right[i] - i)$, then multiply by $arr[i]$, and finally sum these up.
67
+
68
+
Be aware of data overflow and modulo operations.
69
+
70
+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $arr$.
41
71
42
72
<!-- tabs:start -->
43
73
@@ -264,61 +294,4 @@ impl Solution {
264
294
265
295
<!-- tabs:end -->
266
296
267
-
### Solution 2
268
-
269
-
<!-- tabs:start -->
270
-
271
-
```rust
272
-
constMOD:i64= (1e9asi64) +7;
273
-
274
-
implSolution {
275
-
pubfnsum_subarray_mins(arr:Vec<i32>) ->i32 {
276
-
letn:usize=arr.len();
277
-
letmutret:i64=0;
278
-
letmutleft:Vec<i32> =vec![-1; n];
279
-
letmutright:Vec<i32> =vec![nasi32; n];
280
-
// Index stack, store the index of the value in the given array
281
-
letmutstack:Vec<i32> =Vec::new();
282
-
283
-
// Find the first element that's less than the current value for the left side
Copy file name to clipboardexpand all lines: solution/0900-0999/0908.Smallest Range I/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,13 @@
50
50
51
51
## Solutions
52
52
53
-
### Solution 1
53
+
### Solution 1: Mathematics
54
+
55
+
According to the problem description, we can add $k$ to the maximum value in the array and subtract $k$ from the minimum value. This can reduce the difference between the maximum and minimum values in the array.
56
+
57
+
Therefore, the final answer is $\max(nums) - \min(nums) - 2 \times k$.
58
+
59
+
The time complexity is $O(n)$, where $n$ is the length of the array `nums`. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0900-0999/0910.Smallest Range II/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,13 @@
50
50
51
51
## Solutions
52
52
53
-
### Solution 1
53
+
### Solution 1: Greedy + Enumeration
54
+
55
+
According to the problem requirements, we need to find the minimum difference between the maximum and minimum values in the array. Each element can be increased or decreased by $k$, so we can divide the elements in the array into two parts, one part increased by $k$ and the other part decreased by $k$. Therefore, we should decrease the larger values in the array by $k$ and increase the smaller values by $k$ to ensure the minimum difference between the maximum and minimum values.
56
+
57
+
Therefore, we can first sort the array, then enumerate each element in the array, divide it into two parts, one part increased by $k$ and the other part decreased by $k$, and calculate the difference between the maximum and minimum values. Finally, take the minimum value among all differences.
58
+
59
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array.
0 commit comments