Skip to content

Commit abd7113

Browse files
committed
Add maximum subarray solution
1 parent 40601be commit abd7113

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

maximum-subarray/hyunjung-choi.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
fun maxSubArray(nums: IntArray): Int {
3+
var currentSum = nums[0]
4+
var maxSum = nums[0]
5+
6+
for (i in 1 until nums.size) {
7+
currentSum = if (currentSum < 0) nums[i] else currentSum + nums[i]
8+
maxSum = maxOf(currentSum, maxSum)
9+
}
10+
11+
return maxSum
12+
}
13+
}

0 commit comments

Comments
 (0)