不相鄰的最大子數組和
問題描述
給一個數組,數組元素爲不小於零,求和最大的子數組,其中每個元素在原數組中不相鄰。
解題思路
剛拿到題目可能隱約覺得是個dp問題,求前i個元素的最大子數組和,但還是有點手足無措,關鍵是將問題分情況討論:前i個元素的最大子數組包含第i個元素和不包含第i個元素。
- 包含第i個元素,則一定不能包含第i-1個元素,包含第i個元素的最大子數組爲不包含第i-1個元素的最大子數組和加上第i個元素
- 不包含第i個元素,則前i個元素的最大子數組和就是前i-1個元素的最大子數組和
設包含第i個元素的最大子數組和爲dp[i],不包含第i個元素的最大子數組和爲np[i],前i個元素的最大子數組和爲max(dp[i], np[i]),根據上面的分析,則有如下推導公式:
- dp[i] = np[i-1]+arr[i]
- np[i] = max(np[i-1], dp[i-1])
代碼
#include <stdio.h>
#include <stdlib.h>
/*
Problem description:
Find the max sub sum in the arr that each element doesn't neighbor each other.
Elements in the array are not less than 0.
*/
int max_sub_sum(int* arr, int len){
if(len<0)
{
printf("error: array length is less than 0!\n");
return -1;
}
int result = 0;
/*
dp[i]: max sub sum that includes the element i
*/
int* dp = (int*)malloc(sizeof(*dp)*len);
/*
np[i]: max sub sum that excludes the element i
*/
int* np = (int*)malloc(sizeof(*np)*len);
/*
This is a dp problem.
If we use arr[i] to get the result, then we cannot use arr[i-1].
so we get:
dp[i] = np[i-1]+arr[i]
If we don't use arr[i] to get the result, then we the ith max sub sum
equals the (i-1)th max sub sum, for we don't care about whether it includes
arr[i].
so we get:
np[i] = max(dp[i-1], np[i-1])
Time complexity is O(n).
*/
dp[0] = arr[0];
np[0] = 0;
int i=0;
for(i=1; i<len; i++){
dp[i] = np[i-1] + arr[i];
np[i] = dp[i-1]>np[i-1] ? dp[i-1] : np[i-1];
}
result = dp[len-1]>np[len-1] ? dp[len-1] : np[len-1];
free(dp);
free(np);
return result;
}
int main()
{
int arr[8] = {1, 7, 4, 0, 9, 4, 8, 8};
int result = max_sub_sum(arr, 8);
printf("result: %d\n", result);
return 0;
}
codeblocks源碼鏈接:
https://github.com/lilingyu/max_sub_sum