Last active
August 6, 2020 10:38
-
-
Save kimji1/354752b2104ccf068a24964c15ecee66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| println(maxSubArray(intArrayOf(-2,1,-3,4,-1,2,1,-5,4))) | |
| } | |
| fun maxSubArray(nums: IntArray): Int { | |
| var currentMax = nums[0] | |
| var max = nums[0] | |
| for (i in 1 until nums.size) { | |
| currentMax = Math.max(nums[i], nums[i]+currentMax) | |
| max = Math.max(currentMax, max) | |
| } | |
| return max | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://leetcode.com/problems/maximum-subarray/discuss/20211/Accepted-O(n)-solution-in-java