8000 refactor 53 · chandra1123/Leetcode@589a1a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 589a1a2

Browse files
refactor 53
1 parent 11ce76d commit 589a1a2

File tree

1 file changed

+11
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+11
-24
lines changed
Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 53. Maximum Subarray
5-
* Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
6-
*
7-
* Example:
8-
* Input: [-2,1,-3,4,-1,2,1,-5,4],
9-
* Output: 6
10-
* Explanation: [4,-1,2,1] has the largest sum = 6.
11-
*
12-
* Follow up:
13-
* If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
14-
*/
15-
163
public class _53 {
174

185
public static class Solution1 {
19-
/**
20-
* https://leetcode.com/problems/maximum-subarray/discuss/20211/accepted-on-solution-in-java
21-
*/
22-
public int maxSubArray(int[] nums) {
23-
int maxSoFar = nums[0];
24-
int maxEndingHere = nums[0];
25-
for (int i = 1; i < nums.length; i++) {
26-
maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
27-
maxSoFar = Math.max(maxEndingHere, maxSoFar);
6+
/**
7+
* https://leetcode.com/problems/maximum-subarray/discuss/20211/accepted-on-solution-in-java
8+
*/
9+
public int maxSubArray(int[] nums) {
10+
int maxSoFar = nums[0];
11+
int maxEndingHere = nums[0];
12+
for (int i = 1; i < nums.length; i++) {
13+
maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
14+
maxSoFar = Math.max(maxEndingHere, maxSoFar);
15+
}
16+
return maxSoFar;
2817
}
29-
return maxSoFar;
30-
}
3118
}
3219
}

0 commit comments

Comments
 (0)
0