Skip to content

Commit 78de149

Browse files
refactor 209
1 parent a1da55f commit 78de149

File tree

1 file changed

+23
-17
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-17
lines changed
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
4+
* 209. Minimum Size Subarray Sum
5+
*
6+
* Given an array of n positive integers and a positive integer s,
7+
* find the minimal length of a contiguous subarray of which the sum ≥ s.
8+
* If there isn't one, return 0 instead.
59
610
For example, given the array [2,3,1,2,4,3] and s = 7,
711
the subarray [4,3] has the minimal length under the problem constraint.
812
913
click to show more practice.
1014
1115
More practice:
12-
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
16+
If you have figured out the O(n) solution,
17+
try coding another solution of which the time complexity is O(n log n).
1318
*/
1419
public class _209 {
1520

16-
public int minSubArrayLen(int s, int[] nums) {
17-
if (nums == null || nums.length == 0) {
18-
return 0;
19-
}
20-
int i = 0;
21-
int j = 0;
22-
int min = Integer.MAX_VALUE;
23-
int sum = 0;
24-
while (j < nums.length) {
25-
sum += nums[j++];
21+
public static class Solution1 {
22+
public int minSubArrayLen(int s, int[] nums) {
23+
if (nums == null || nums.length == 0) {
24+
return 0;
25+
}
26+
int i = 0;
27+
int j = 0;
28+
int min = Integer.MAX_VALUE;
29+
int sum = 0;
30+
while (j < nums.length) {
31+
sum += nums[j++];
2632

27-
while (sum >= s) {
28-
min = Math.min(min, j - i);
29-
sum -= nums[i++];
33+
while (sum >= s) {
34+
min = Math.min(min, j - i);
35+
sum -= nums[i++];
36+
}
3037
}
38+
return min == Integer.MAX_VALUE ? 0 : min;
3139
}
32-
return min == Integer.MAX_VALUE ? 0 : min;
3340
}
34-
3541
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy