Skip to content

Commit e1b93d6

Browse files
refactor 53
1 parent c20f58d commit e1b93d6

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/main/java/com/fishercoder/solutions/_53.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public static class Solution1 {
77
* https://leetcode.com/problems/maximum-subarray/discuss/20211/accepted-on-solution-in-java
88
*/
99
public int maxSubArray(int[] nums) {
10-
int maxSoFar = nums[0];
11-
int maxEndingHere = nums[0];
10+
int globalMax = nums[0];
11+
int currentMax = nums[0];
1212
for (int i = 1; i < nums.length; i++) {
13-
maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
14-
maxSoFar = Math.max(maxEndingHere, maxSoFar);
13+
currentMax = Math.max(nums[i], currentMax + nums[i]);
14+
globalMax = Math.max(currentMax, globalMax);
1515
}
16-
return maxSoFar;
16+
return globalMax;
1717
}
1818
}
1919
}

src/test/java/com/fishercoder/_53Test.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
import static org.junit.Assert.assertEquals;
99

1010
public class _53Test {
11-
private static _53.Solution1 solution1;
12-
private static int[] nums;
11+
private static _53.Solution1 solution1;
12+
private static int[] nums;
1313

14-
@BeforeClass
15-
public static void setup() {
16-
solution1 = new _53.Solution1();
17-
}
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _53.Solution1();
17+
}
1818

19-
@Before
20-
public void clear() {
21-
solution1 = new _53.Solution1();
22-
}
19+
@Before
20+
public void clear() {
21+
solution1 = new _53.Solution1();
22+
}
2323

24-
@Test
25-
public void test1() {
26-
nums = new int[] {-2, 1, -3, 4, -1, 2, 1, -5, 4};
27-
assertEquals(6, solution1.maxSubArray(nums));
28-
}
24+
@Test
25+
public void test1() {
26+
nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
27+
assertEquals(6, solution1.maxSubArray(nums));
28+
}
2929
}

0 commit comments

Comments
 (0)