Skip to content

Commit 0197816

Browse files
committed
maximum-subarray solved
1 parent 32a53ec commit 0197816

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

maximum-subarray/kut7728.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
///53. Maximum Subarray
2+
///정수 배열 Nums가 주어질때, subarray들 중에서 가장 합이 큰걸 구하고 그 합을 반환하라
3+
///subarray: 비지 않은 연속된 요소들의 배열
4+
5+
6+
class Solution {
7+
func maxSubArray(_ nums: [Int]) -> Int {
8+
guard !nums.isEmpty else { return 0 }
9+
10+
var currentSum = nums[0]
11+
var maxSum = nums[0]
12+
13+
for num in nums.dropFirst() {
14+
currentSum = max(num, currentSum + num)
15+
maxSum = max(maxSum, currentSum)
16+
}
17+
18+
return maxSum
19+
}
20+
}

0 commit comments

Comments
 (0)