Max Number of Coins (Coding Problem By Zillow)

 

You are given a 2-d matrix where each cell represents number of coins in that cell. Assuming we start at matrix[0][0], and can only move right or down, find the maximum number of coins you can collect by the bottom right corner.

For example, in this matrix

0 3 1 1
2 0 0 4
1 5 3 1


The most we can collect is 0 + 2 + 1 + 5 + 3 + 1 = 12 coins.

Solution:
 https://github.com/cbsingh1/DailyCodingProblems/blob/main/src/main/java/MaxCoinCollector_838.java


public class MaxCoinCollector_838 {
static int findMaxCoins(int coinMatrix[][]) {

if (coinMatrix == null) return 0;

int rows = coinMatrix.length;
int columns = coinMatrix[0].length;
int[][] tempMatrix = new int[rows] [columns];

tempMatrix[0] [0] = coinMatrix[0] [0];

for (int c = 1; c < columns; c++)
tempMatrix[0] [c] = tempMatrix[0] [c - 1] + coinMatrix[0] [c];

for (int r = 1; r < rows; r++)
tempMatrix[r] [0] = tempMatrix[r - 1] [0] + coinMatrix[r] [0];

for (int r = 1; r < rows; r++)
for (int c = 1; c < columns; c++)
tempMatrix[r] [c] = Math.max(tempMatrix[r - 1] [c], tempMatrix[r] [c - 1]) + coinMatrix[r] [c];

return tempMatrix[rows - 1] [columns - 1];
}

//Drive Program
public static void main(String[] args) {

int [][] coinMatrix = {{0,3, 1, 1}, {2,0, 0, 4}, {1,5,3,1}};
System.out.println("Max coin : " + findMaxCoins(coinMatrix));
}
}

Comments

Popular posts from this blog

Sliding Window Maximum (Maximum of all subarrays of size k) - Java Solution - Leetcode 239

Count occurrence of anagrams in String