[LeetCode] Maximum Subarray
1. ๋ฌธ์ : Link
ํ์ array ์ค ํฉ์ ์ต๋๊ฐ ์ฐพ๋ ๋ฌธ์
2. ํ์ด
array๋ฌธ์ ์ ํ์ ์๊ธธ๋ dp ์๊ฐ๋ ๋ชปํ๋๋ฐ dp ๋ฌธ์ ์ฌ๋ฐ...
3. ์ฝ๋
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = [num for num in nums]
for i in range(1, len(nums)):
dp[i] = max(dp[i-1]+nums[i], nums[i])
return max(dp)
class Solution {
public int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
for(int i=0; i<nums.length; i++){
dp[i] = nums[i];
}
for(int i=1; i<nums.length; i++){
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
}
int max = Integer.MIN_VALUE;
for(int i=0; i<nums.length; i++){
if(dp[i] > max) max = dp[i];
}
return max;
}
}
'๐ป Coding Problems Solving > Dynamic Programming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ ๋ฐ๋จน๊ธฐ (์๋ฐ java) (0) | 2023.03.10 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฉ๋ฆฌ ๋ฐ๊ธฐ (์๋ฐ java) (0) | 2023.01.12 |
[LeetCode] Climbing Stairs (0) | 2022.07.09 |
[BOJ 15486] ํด์ฌ2 (0) | 2022.07.05 |
[BOJ 2294] ๋์ 2 (0) | 2022.06.29 |
์ต๊ทผ๋๊ธ