Sherlock and Cost Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)usingnamespacestd;typedeflonglongll;llmod=1e9+7;lldp[100001][101];llFun(llidx,lln,vector<ll>&B,llprevious){if(idx==n)return0;if(dp[idx][previous]!=-1)returndp[idx][previous];llans=0,left=0;if(idx==0)ans=max(Fun(idx+1,n,B,1),Fun(idx+1,n,B,B[idx]));else{left=max(abs(previous-1)+Fun(idx+1,n,B,1),abs(previous-B[idx])+Fun(idx+1,n,B,B[idx]));ans=max(ans,left);}returndp[idx][previous]=ans;}voidDon(){lln;cin>>n;vector<ll>B(n);for(inti=0;i<n;i++)cin>>B[i];memset(dp,-1,sizeof(dp));llans=Fun(0,n,B,0);cout<<ans<<endl;}intmain(){llt;cin>>t;while(t--){Don();}}/*instead of trying to place A[i] from 1 to B[i], just try 1 or B[i] because 1-B[i] gives us maximum.here is my solution using recursion to try all possible ways of keeping either 1 or B[i]after that i added memoization */
Sherlock and Cost
You are viewing a single comment's thread. Return to all comments →