Question :Coin CollectionRob is fond of collecting coins but unfortunately, sometimes he has to sell acoin to get other coins. He puts all his coins one over the other in a column.There can be 2 types of operations:(0, i): Add coin with value i to the collection.(1.0): Sell a coinSelling a coin:For selling a coin, he picks up the top coin in the column if the constraint A issatisfied.Constraint A: Rob can only sell the coin if the coin collection value is morethan Y.Upon removal, the coin collection value goes to the previous value. Initially,with O coins, the coin collection value is 0.Adding a coin:On adding a coin of value i to the collection the coin collection valueincrements via the following nrocedure
Answers
Answer:
10(ten)
Explanation:
because this answer is correct or wrong.
Answer:
Maximum number of collected coins is 8
Explanation:
#include<bits/stdc++.h>
using namespace std;
#define R 5
#define C 5
bool isValid(int i, int j)
{
return (i >=0 && i < R && j >=0 && j < C);
}
int maxCoinsUtil(char arr[R][C], int i, int j, int dir,
int dp[R][C][2])
{
if (isValid(i,j) == false || arr[i][j] == '#')
return 0;
if (dp[i][j][dir] != -1)
return dp[i][j][dir];
dp[i][j][dir] = (arr[i][j] == 'C')? 1: 0;
if (dir == 1)
dp[i][j][dir] += max(maxCoinsUtil(arr, i+1, j, 0, dp),
maxCoinsUtil(arr, i, j+1, 1, dp));
if (dir == 0)
dp[i][j][dir] += max(maxCoinsUtil(arr, i+1, j, 1, dp),
maxCoinsUtil(arr, i, j-1, 0, dp));
return dp[i][j][dir];
}
int maxCoins(char arr[R][C])
{
int dp[R][C][2];
memset(dp, -1, sizeof dp);
return maxCoinsUtil(arr, 0, 0, 1, dp);
}
int main()
{
char arr[R][C] = { {'E', 'C', 'C', 'C', 'C'},
{'C', '#', 'C', '#', 'E'},
{'#', 'C', 'C', '#', 'C'},
{'C', 'E', 'E', 'C', 'E'},
{'C', 'E', '#', 'C', 'E'}
};
cout << "Maximum number of collected coins is "
<< maxCoins(arr);
return 0;
}
The above code is a c++ code for counting maximum number of coins collected by a person according the set input.
SPJ3