Comments (1)

Reputation 10

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

***********************Explanation***********************

- nikitakumari01021998 July 21, 2020We have two options from given stack -> i) Do not take any element from ith stack

ii) Take elements from ith stack.

We will use dp here.

Definition of dp -> choose j elements from ith stack.

State of dp -> dp[i][j] = max(dp[i][j], dp[i-1][0,1,2...k]+(sum of top j elements from stack i)

**********************Code*************************

int sum(int stackI[], int p, int n)

{

int j=n-1;

int sumOfTopElement=0;

while(p)

{

sumOfTopElement += stackI[j--];

p--;

}

}

void solution(int stack[][],int K)

{

int dp[stack.size()][K+1];

memset(dp, 0, sizeof dp);

for(int k=1;k<=K;k++)

{

if(stack[0].size()>=k)

dp[i][k]=sum(stack[0],k,stack[0].size());

}

for(int i=1;i<n;i++)

{

for(int k=1;k<=K;k++)

{

dp[i][k]=dp[i-1][k];

for(int j=1;j<=k;j++)

{

if(stack[i].size()>=j)

dp[i][k]=max(dp[i][k],dp[i-1][k-j]+sum(stack[i],j,stack[i].size()));

}

}

}

}