문제 정보
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.
문제 풀이
단순하게 모든 부분합을 반복하며 최대값을 구해 저장할 수 도 있겠지만 이는 O(n^2) 시간복잡도를 가진다.
DP나 카데인 알고리즘을 사용하여 풀이하면 O(n)으로 해결할 수 있다.
Kadane's algorithm 이란?
The simple idea of Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). Each time we get a positive-sum compare it with max_so_far and update max_so_far if it is greater than max_so_far
Kadane's algorithm은 DP알고리즘의 일종으로 부분합의 최대값(Local maximum)에서 이후 엔트리가 양인 경우만 새로운 local maximum으로 지정해가면서 값을 갱신해나가면 된다.
Kadane's algorithm
Initialize:
max_so_far = INT_MIN
max_ending_here = 0
Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_so_far < max_ending_here)
max_so_far = max_ending_here
(c) if(max_ending_here < 0)
max_ending_here = 0
return max_so_far
The simple idea of Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). Each time we get a positive-sum compare it with max_so_far and update max_so_far if it is greater than max_so_far
Lets take the example:
{-2, -3, 4, -1, -2, 1, 5, -3}
max_so_far = max_ending_here = 0
for i=0, a[0] = -2
max_ending_here = max_ending_here + (-2)
Set max_ending_here = 0 because max_ending_here < 0
for i=1, a[1] = -3
max_ending_here = max_ending_here + (-3)
Set max_ending_here = 0 because max_ending_here < 0
for i=2, a[2] = 4
max_ending_here = max_ending_here + (4)
max_ending_here = 4
max_so_far is updated to 4 because max_ending_here greater
than max_so_far which was 0 till now
for i=3, a[3] = -1
max_ending_here = max_ending_here + (-1)
max_ending_here = 3
for i=4, a[4] = -2
max_ending_here = max_ending_here + (-2)
max_ending_here = 1
for i=5, a[5] = 1
max_ending_here = max_ending_here + (1)
max_ending_here = 2
for i=6, a[6] = 5
max_ending_here = max_ending_here + (5)
max_ending_here = 7
max_so_far is updated to 7 because max_ending_here is
greater than max_so_far
for i=7, a[7] = -3
max_ending_here = max_ending_here + (-3)
max_ending_here = 4
Writeup (using DP)
public class Solution
{
public int MaxSubArray(int[] nums)
{
int[] local_maximum = new int[nums.Length];
int global_maximum;
global_maximum = nums[0];
local_maximum[0] = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (i != 0)
{
local_maximum[i] = local_maximum[i - 1] + nums[i];
}
if (local_maximum[i] > global_maximum)
{
global_maximum = local_maximum[i];
}
if (local_maximum[i] < 0)
{
local_maximum[i] = 0;
}
}
return global_maximum;
}
}
Writeup (using Kadane's algorithm)
static int maxSubArraySum(int[] a, int size)
{
int max_so_far = a[0], max_ending_here = 0;
for (int i = 0; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;
/* Do not compare for all
elements. Compare only
when max_ending_here > 0 */
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
출처 : https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
Largest Sum Contiguous Subarray - GeeksforGeeks
Maximum sum contiguous subarray within a one-dimensional array of numbers using Kadane's Algorithm
www.geeksforgeeks.org
'Software Engineering > Algorithm' 카테고리의 다른 글
[리트코드] 94. Binary Tree Inorder Traversal (tree, recursion) (0) | 2022.01.27 |
---|---|
[리트코드] 62. Unique Paths (DP) (0) | 2022.01.27 |
[리트코드] 49. Group Anagrams (HashTable) (0) | 2022.01.26 |
[리트코드] 2011. Final Value of Variable After Performing Operations (string) (0) | 2022.01.26 |
[리트코드] 1. TwoSum (array) (0) | 2022.01.26 |
댓글