forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
36 lines (29 loc) · 969 Bytes
/
Solution2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// https://leetcode.com/problems/minimum-size-subarray-sum/description/
/// Author : liuyubobobo
/// Time : 2019-03-03
// Brute Force + Greedy
// Time Complexity: O(n^2)
// Space Complexity: O(1)
public class Solution2 {
public int minSubArrayLen(int s, int[] nums) {
if(s <= 0 || nums == null)
throw new IllegalArgumentException("Illigal Arguments");
int res = nums.length + 1;
for(int l = 0 ; l < nums.length ; l ++) {
int sum = 0;
for (int r = l; r < nums.length; r++){
sum += nums[r];
if(sum >= s){
res = Math.min(res, r - l + 1);
break;
}
}
}
return res == nums.length + 1 ? 0 : res;
}
public static void main(String[] args) {
int[] nums = {2, 3, 1, 2, 4, 3};
int s = 7;
System.out.println((new Solution2()).minSubArrayLen(s, nums));
}
}