forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
이연수
committed
Jan 3, 2025
1 parent
dafbd2f
commit 7e17953
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* 주어진 숫자 배열에서 Subarray가 가장 큰 수를 구하는 문제 | ||
* 시간 복잡도: O(n) | ||
* -> 주어진 배열만큼 계산 | ||
* 공간 복잡도: O(n) | ||
* -> 가중치를 더하는 배열 필요 | ||
* */ | ||
fun maxSubArray(nums: IntArray): Int { | ||
val dp = IntArray(nums.size) | ||
dp[0] = nums[0] | ||
|
||
for (i in 1 until nums.size) { | ||
if (dp[i - 1] + nums[i] >= 0 && dp[i-1] >= 0) { | ||
dp[i] = dp[i - 1] + nums[i] | ||
} else { | ||
dp[i] = nums[i] | ||
} | ||
} | ||
return dp.max() | ||
} |