Created
April 3, 2020 21:27
-
-
Save ChrisGuzman/bf095baa0cb60d9bdeac20d429644f05 to your computer and use it in GitHub Desktop.
LeetCode #3 Maximum Subarray
This file contains 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(args: Array<String>) { | |
maxSubArray(intArrayOf(-2,1,-3,4,-1,2,1,-5,4)) | |
} | |
fun maxSubArray(nums: IntArray): Int { | |
val maxSums = mutableListOf<Int>() | |
nums.toList() | |
var i = 0 | |
while (i < nums.size) { | |
val subList = nums.toList().subList(i, nums.size) | |
// println(subList) | |
maxSums.add(findMaxFromIndex(subList)) | |
i++ | |
} | |
println(maxSums) | |
return maxSums.max()!! | |
} | |
fun findMaxFromIndex(numList: List<Int>): Int { | |
var index = 0 | |
var end = index + 1 | |
val sums = mutableListOf<Int>() | |
var i = 0 | |
while (i < numList.size) { | |
var sum = numList.subList(index, end).sum() | |
sums.add(sum) | |
// println(Pair(index, end)) | |
// println(sum) | |
end++ | |
i++ | |
} | |
val maxSum = sums.max()!! | |
// println(maxSum) | |
return maxSum | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment