#include <bits/stdc++.h> int maximumNonAdjacentSum(vector<int> &nums){ // Write your code here. int n=nums.size(); int prev2=0; int pre=nums[0],curri=0; for(int i=1;i<n;i++) { int pick=nums[i]; if(i>1) pick+=prev2; int notp=0+pre; curri=max(pick,notp); prev2=pre; pre=curri; } return pre; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter