Maximize value of coins from adjacent row and columns cannot to be collected


Dynamic programming is an optimization algorithmic technique to solve the particular problems by splitting them into some simple sub problems. It is a procedure by which we can combine the quality, condition, or fact of a complete search for a greedy algorithm being exact and accurate. But this method is an oxymoron itself because it comes with a great advantage and that is also its biggest disadvantage and limitation too. We can divide a problem into some sub problem but we can't divide the sub problems again. They should be solvable by its own. The concept of the sub problems can be used to solve more significant problems as they are highly optimized in nature.

What is coin and how to change it?

The coins are the components of an array which represents the sum of the integers of the total amount of money. In this process you should return some few coins to balance the sum. If it is not constructed then we get -1 as return.

There are two solutions to Change the coin −

  • Recursion - Naive and slow approach.

  • Dynamic Programming - A timely and efficient approach

Applications of a coin in computer science −

  • Used to distribute a change.

Algorithm of a coin operation

Here is the step by step process how we can maximize the value of coins from an adjacent row.

  • Step 1 − Start

  • Step 2 − Build a new array with a length of n+1

  • Step 3 − Set dynamic prog[0] to 1 for one way process

  • Step 4 − Iterate the value

  • Step 5 − Add the value of dynamicprog[index-coins[i]] to dynamicprog[index]

  • Step 6 − Set a range from 1 to n

  • Step 7 − Retun a value

  • Step 8 − Terminate

Syntax of a coin

If the coin value is greater than the dynamicprogSum, the coin is ignored, i.e.
dynamicprogTable[i][j]=dynamicprogTable[i-1][j].
If the coin value is less than the dynamicprogSum, you can consider it, i.e.
dynamicprogTable[i][j]=dynamicprogTable[i-
1].[dynamicprogSum]+dynamicprogTable[i][j-coins[i-1]].
Or;
maxCoins(i, j, d): Maximum number of coins that can be
collected if we begin at cell (i, j) and direction d.
   d can be either 0 (left) or 1 (right)
If (arr[i][j] == ‘#’ or isValid(i, j) == false)
   return 0
If (arr[i][j] == ‘C’)
   result = 1;
Else
   result = 0;
If (d == 0)
return result + max(maxCoins(i+1, j, 1),
   maxCoins(i, j-1, 0));
If (d == 1)
return result + max(maxCoins(i+1, j, 1),
   maxCoins(i, j+1, 0));

Here the possible syntax of the coin change in a C++ environment. By applying this syntax we are going to bulid some codes to get a broad view of this coin.

Approaches to follow:-

  • Approach 1 − Recursive C++ program to find maximum number of coins

  • Approach 2 − Maximize value of coins when coins from adjacent row and columns cannot be collected

Recursive C++ program to find maximum number of coins

In this code, we have applied the dynamic programming. The logic is : arr[i][j + 1] and arr[i][j – 1].

Example 1

#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 maxCoinsRec(char arr[R][C], int i, int j, int dir){
   if (isValid(i,j) == false || arr[i][j] == '#')
      return 0;
   int result = (arr[i][j] == 'C')? 1: 0;
   if (dir == 1)
      return result + max(maxCoinsRec(arr, i+1, j, 0),
   maxCoinsRec(arr, i, j+1, 1));
   return result + max(maxCoinsRec(arr, i+1, j, 1),
   maxCoinsRec(arr, i, j-1, 0));
}
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 "<< maxCoinsRec(arr, 0, 0, 1);
   return 0;
}

Output

Maximum number of collected coins is 8

Maximize value of coins when coins from adjacent row and columns cannot be collected

In this C++ code, we have applied the method to find and collect maximum coins before hitting a dead end.

  • Move one step ahead, i.e., cell (i, j+1) and direction remains right.

  • Move one step down and face left, i.e., cell (i+1, j) and direction becomes left.

Example 2

#include <bits/stdc++.h>
using namespace std;
int findMax(vector<int>& arr) {
   int n = arr.size(), result = 0;
   vector<int> dp(n);
   dp[0] = arr[0];
   result = dp[0];
   if (n <= 1)
      return result;
   dp[1] = max(arr[1], arr[0]);
   result = max(result, dp[1]);
   for (int i = 2; i < n; i++) {
      dp[i] = max(dp[i - 1], arr[i] + dp[i - 2]);
      result = max(result, dp[i]);
   }
   return result;
}
int solve(vector<vector<int> >& matrix){
   int m = matrix.size();
   if (m == 0)
      return 0;
   vector<int> dp;
   for (int i = 0; i < m; i++) {
      int val = findMax(matrix[i]);
   dp.push_back(val);
   }
   return findMax(dp);
}
int main() {
   vector<vector<int> > arr = { { 2, 7, 6, 5 },
      { 9, 9, 1, 2 },
      { 3, 8, 1, 5 } };
   int result = solve(arr);
   cout << result;
   return 0;
}

Output

25

Conclusion

Today in this article we have learnt how to maximize a value of coins from the adjacent row where columns cannot to be collected, with the help of possible C++ build code and algorithm.

Updated on: 05-Apr-2023

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements