Today I learned
2022/02/09
회고록
항해 99, 알고리즘 4주차(항해 5주차)
교재 : 파이썬 알고리즘 인터뷰 / 이것이 코딩테스트다(동빈좌)
다이나믹 프로그래밍
이론 정리 포스팅 글(내 벨로그)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
A subarray is a contiguous part of an array.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
https://leetcode.com/problems/maximum-subarray/
nums = [-2,1,-3,4,-1,2,1,-5,4]
d = [0]*len(nums)
d[0] = -2
d[1] = 1
for i in range(2, len(nums)):
if d[i-1] <= 0:
d[i] = nums[i]
else:
d[i] = nums[i] + d[i-1]
print(f'{max(d)}')
DP 알고리즘 익히기