diff options
author | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
---|---|---|
committer | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
commit | 6522012065712fb0ece31bff9ff10b38a83b10e1 (patch) | |
tree | 743f3c13d08be9f60bad57b74e7a1fe7cc675e89 /MaxSubArray.java |
Initial commit
Diffstat (limited to 'MaxSubArray.java')
-rw-r--r-- | MaxSubArray.java | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/MaxSubArray.java b/MaxSubArray.java new file mode 100644 index 0000000..89698c2 --- /dev/null +++ b/MaxSubArray.java @@ -0,0 +1,19 @@ +import java.lang.*; +import java.util.*; + +class MaxSubArray { + public static int maxSubArray(int[] nums) { + int currSum = 0; + int maxSum = 0; + for (int i : nums) { + currSum = Math.max(currSum + i, 0); + maxSum = Math.max(maxSum, currSum); + } + return maxSum; + } + + public static void main(String[] args) { + int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; + System.out.println(maxSubArray(nums)); + } +} |