Skip to content

Commit 6342e82

Browse files
committed
[zh-tw] Add Minimum Subarray
1 parent 232f096 commit 6342e82

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Minimum Subarray
2+
3+
## Question
4+
5+
- lintcode: [(44) Minimum Subarray](http://www.lintcode.com/en/problem/minimum-subarray/)
6+
7+
```
8+
Given an array of integers, find the subarray with smallest sum.
9+
10+
Return the sum of the subarray.
11+
12+
Example
13+
For [1, -1, -2, 1], return -3
14+
15+
Note
16+
The subarray should contain at least one integer.
17+
```
18+
19+
## 題解
20+
21+
題目 [Maximum Subarray](http://algorithm.yuanbin.me/zh-hans/dynamic_programming/maximum_subarray.html) 的變形,使用區間和容易理解和實現。
22+
23+
### Java
24+
25+
```java
26+
public class Solution {
27+
/**
28+
* @param nums: a list of integers
29+
* @return: A integer indicate the sum of minimum subarray
30+
*/
31+
public int minSubArray(ArrayList<Integer> nums) {
32+
if (nums == null || nums.isEmpty()) return -1;
33+
34+
int sum = 0, maxSum = 0, minSub = Integer.MAX_VALUE;
35+
for (int num : nums) {
36+
maxSum = Math.max(maxSum, sum);
37+
sum += num;
38+
minSub = Math.min(minSub, sum - maxSum);
39+
}
40+
41+
return minSub;
42+
}
43+
}
44+
```
45+
46+
### 源碼分析
47+
48+
49+
50+
### 複雜度分析
51+
52+

0 commit comments

Comments
 (0)