Given a square matrix of size N*N, where each cell is associated with a specific cost. A path is defined as a specific sequence of cells which starts from the top left cell move only right or down and ends on bottom right cell. We want to find a path with the maximum average over all existing paths. Average is computed as total cost divided by the number of cells visited in the path.
Input : Matrix = [1, 2, 3 4, 5, 6 7, 8, 9] Output : 5.8 Path with maximum average is, 1 -> 4 -> 7 -> 8 -> 9 Sum of the path is 29 and average is 29/5 = 5.8
One interesting observation is, the only allowed moves are down and right, we need N-1 down moves and N-1 right moves to reach the destination (bottom rightmost). So any path from top left corner to bottom right corner requires 2N – 1 cells. In average value, the denominator is fixed and we need to just maximize numerator. Therefore we basically need to find the maximum sum path. Calculating maximum sum of path is a classic dynamic programming problem, if dp[i][j] represents maximum sum till cell (i, j) from (0, 0) then at each cell (i, j), we update dp[i][j] as below,
for all i, 1 <= i <= N dp[i] = dp[i-1] + cost[i]; for all j, 1 <= j <= N dp[j] = dp[j-1] + cost[j]; otherwise dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + cost[i][j];
Once we get maximum sum of all paths we will divide this sum by (2N – 1) and we will get our maximum average.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum decimal value path in a binary matrix
- Maximum average sum partition of an array
- Minimum cost path in a Matrix by moving only on value difference of X
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Sum of average of all subsets
- Number of ways to choose elements from the array such that their average is K
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Maximum weight path ending at any element of last row in a matrix
- Maximum path sum for each position with jumps under divisibility condition
- Maximum path sum in a triangle.
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Maximum sum of a path in a Right Number Triangle
- Maximum path sum in matrix
- Maximum sum path in a matrix from top to bottom
- Maximum path sum in the given arrays with at most K jumps
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Find maximum path length in a binary matrix
- Maximum path sum in an Inverted triangle | SET 2
- Maximum sum path in a matrix from top to bottom and back
- Paths from entry to exit in matrix and maximum path sum