[Coding Made Simple] Maximum Sum Subsequence Non-adjacent

Given an array of positive number, find maximum sum subsequence such that elements in this subsequence are not adjacent to each other.

Recursive formula: f(n) = Math.max{f(n - 1), f(n - 2) + arr[n - 1]}.

Dynamic programming is used to get rid of the overlapping subproblems.

State: T[i]: the max sum of non-adjacent subsequence from arr[0.... n - 1];

Function: T[i] = Math.max(T[i - 1], T[i - 2] + arr[i - 1]);

Init: T[0] = 0, T[1] = arr[0];

Answer: T[arr.length].

 1 import java.util.ArrayList;
 2
 3 public class MaxSumSubsequence {
 4     private ArrayList<Integer> subseq;
 5     public int getMaxSumSubseqNonAdj(int[] arr) {
 6         if(arr == null || arr.length == 0) {
 7             return 0;
 8         }
 9         int[] T = new int[arr.length + 1];
10         T[0] = 0; T[1] = arr[0];
11         for(int i = 2; i < T.length; i++) {
12             T[i] = Math.max(T[i - 1], T[i - 2] + arr[i - 1]);
13         }
14         subseq = new ArrayList<Integer>();
15         int currSum = T[arr.length];
16         int idx = arr.length;
17         while(currSum != 0) {
18             if(idx >= 2) {
19                 if(T[idx - 1] <= T[idx - 2] + arr[idx - 1]) {
20                     subseq.add(arr[idx - 1]);
21                     currSum -= arr[idx - 1];
22                     idx -= 2;
23                 }
24                 else {
25                     idx--;
26                 }
27             }
28             else {
29                 subseq.add(arr[idx - 1]);
30                 currSum -= arr[idx - 1];
31                 idx--;
32             }
33         }
34         return T[arr.length];
35     }
36 }
时间: 2024-11-07 03:13:26

[Coding Made Simple] Maximum Sum Subsequence Non-adjacent的相关文章

[Coding Made Simple] Maximum Subsquare surrounded by &#39;X&#39;

Given a 2D matrix where every element is either 'O' or 'X', find the largest subsquare surrounded by 'X'. Examples: Input: mat[N][N] = { {'X', 'O', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'X', 'O'}, {'X', 'X', 'X', 'X', 'X'}, {'X',

[Coding Made Simple] Longest Increasing Subsequence

Given an array, find the longest increasing subsequence in this array. The same problem link. [LintCode] Longest Increasing Subsequence

[Coding Made Simple] Longest Bitonic Subsequence

Find longest bitonic subsequence in given array. Bitonic subsequence first increases then decreases. Same problem link Longest Bitonic Subsequence

[Coding Made Simple] Subset Sum Problem

Given a set of non negative integers and a target value, find if there exists a subset in the given set whose sum is target. Solution 1. Enumerate all possible subsets and check if their sum is the target The runtime of this solution is O(2^n).  This

[Coding Made Simple] Box Stacking

Given boxes of different dimensions, stack them on top of each other to get maximum height such that box on top has strictly less length and width than box under it. Algorithm. 1. get all the box permutation with each permutation's length >= width; s

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

UVA - 108 - Maximum Sum (简单贪心)

UVA - 108 Maximum Sum Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension.

1481:Maximum sum

1481:Maximum sum 总时间限制:  1000ms 内存限制:  65536kB 描述 Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: t1 t2 d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n } i=s1 j=s2 Your task is to calculate d(A). 输入

maximum sum

uva,10684 1 #include <iostream> 2 #include <cstdio> 3 #define maxn 10005 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 int a[maxn]; 10 while(scanf("%d",&n),n) 11 { 12 for(int i=0;i<n;i++) 13 scanf("%d",&a[