[InterviewBit] Max Sum Without Adjacent Elements

Max Sum Without Adjacent Elements

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>

int Solution::adjacent(vector<vector<int>> &A) {
int dp = 0, dpp = 0;
for(int i = 0; i < A[0].size(); i++) {
int now = max(A[0][i], A[1][i]);
int dppp = max(dp, dpp + now);
dpp = dp;
dp = dppp;
}
return max(dp,dpp);
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/07/PS/interviewbit/max-sum-without-adjacent-elements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.