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 that 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.
Examples:
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][0] = dp[i-1][0] + cost[i][0];
for all j, 1 <= j <= N
dp[0][j] = dp[0][j-1] + cost[0][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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
const int M = 100;
double maxAverageOfPath(int cost[M][M], int N)
{
int dp[N+1][N+1];
dp[0][0] = cost[0][0];
for (int i = 1; i < N; i++)
dp[i][0] = dp[i-1][0] + cost[i][0];
for (int j = 1; j < N; j++)
dp[0][j] = dp[0][j-1] + cost[0][j];
for (int i = 1; i < N; i++)
for (int j = 1; j <= N; j++)
dp[i][j] = max(dp[i-1][j],
dp[i][j-1]) + cost[i][j];
return (double)dp[N-1][N-1] / (2*N-1);
}
int main()
{
int cost[M][M] = { {1, 2, 3},
{6, 5, 4},
{7, 3, 9}
};
printf("%f", maxAverageOfPath(cost, 3));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static double maxAverageOfPath(int cost[][],
int N)
{
int dp[][] = new int[N+1][N+1];
dp[0][0] = cost[0][0];
for (int i = 1; i < N; i++)
dp[i][0] = dp[i-1][0] + cost[i][0];
for (int j = 1; j < N; j++)
dp[0][j] = dp[0][j-1] + cost[0][j];
for (int i = 1; i < N; i++)
for (int j = 1; j < N; j++)
dp[i][j] = Math.max(dp[i-1][j],
dp[i][j-1]) + cost[i][j];
return (double)dp[N-1][N-1] / (2 * N - 1);
}
public static void main(String[] args)
{
int cost[][] = {{1, 2, 3},
{6, 5, 4},
{7, 3, 9}};
System.out.println(maxAverageOfPath(cost, 3));
}
}
|
Python3
M = 100
def maxAverageOfPath(cost, N):
dp = [[0 for i in range(N + 1)] for j in range(N + 1)]
dp[0][0] = cost[0][0]
for i in range(1, N):
dp[i][0] = dp[i - 1][0] + cost[i][0]
for j in range(1, N):
dp[0][j] = dp[0][j - 1] + cost[0][j]
for i in range(1, N):
for j in range(1, N):
dp[i][j] = max(dp[i - 1][j],
dp[i][j - 1]) + cost[i][j]
return dp[N - 1][N - 1] / (2 * N - 1)
cost = [[1, 2, 3],
[6, 5, 4],
[7, 3, 9]]
print(maxAverageOfPath(cost, 3))
|
C#
using System;
class GFG {
public static double maxAverageOfPath(int [,]cost,
int N)
{
int [,]dp = new int[N+1,N+1];
dp[0,0] = cost[0,0];
for (int i = 1; i < N; i++)
dp[i, 0] = dp[i - 1,0] + cost[i, 0];
for (int j = 1; j < N; j++)
dp[0, j] = dp[0,j - 1] + cost[0, j];
for (int i = 1; i < N; i++)
for (int j = 1; j < N; j++)
dp[i, j] = Math.Max(dp[i - 1, j],
dp[i,j - 1]) + cost[i, j];
return (double)dp[N - 1, N - 1] / (2 * N - 1);
}
public static void Main()
{
int [,]cost = {{1, 2, 3},
{6, 5, 4},
{7, 3, 9}};
Console.Write(maxAverageOfPath(cost, 3));
}
}
|
PHP
<?php
function maxAverageOfPath($cost, $N)
{
$dp = array(array()) ;
$dp[0][0] = $cost[0][0];
for ($i = 1; $i < $N; $i++)
$dp[$i][0] = $dp[$i-1][0] + $cost[$i][0];
for ($j = 1; $j < $N; $j++)
$dp[0][$j] = $dp[0][$j-1] + $cost[0][$j];
for ($i = 1; $i < $N; $i++)
{
for ($j = 1; $j <= $N; $j++)
$dp[$i][$j] = max($dp[$i-1][$j],$dp[$i][$j-1]) + $cost[$i][$j];
}
return $dp[$N-1][$N-1] / (2*$N-1);
}
$cost = array(array(1, 2, 3),
array( 6, 5, 4),
array(7, 3, 9) ) ;
echo maxAverageOfPath($cost, 3) ;
?>
|
Javascript
<script>
function maxAverageOfPath(cost, N)
{
let dp = new Array(N+1);
for (let i = 0; i < N + 1; i++)
{
dp[i] = new Array(N + 1);
for (let j = 0; j < N + 1; j++)
{
dp[i][j] = 0;
}
}
dp[0][0] = cost[0][0];
for (let i = 1; i < N; i++)
dp[i][0] = dp[i-1][0] + cost[i][0];
for (let j = 1; j < N; j++)
dp[0][j] = dp[0][j-1] + cost[0][j];
for (let i = 1; i < N; i++)
for (let j = 1; j < N; j++)
dp[i][j] = Math.max(dp[i-1][j],
dp[i][j-1]) + cost[i][j];
return dp[N-1][N-1] / (2 * N - 1);
}
let cost = [[1, 2, 3],
[6, 5, 4],
[7, 3, 9]];
document.write(maxAverageOfPath(cost, 3));
</script>
|
Time complexity: O(N2) for given input N
Auxiliary Space: O(N2) for given input N.
Method – 2: Without using Extra N*N space
We can use input cost array as a dp to store the ans. so this way we don’t need an extra dp array or no that extra space.\
One observation is that 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 cell. 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, also we don’t need any prev cost[i][j] value after calculating dp[i][j] so we can modifies the cost[i][j] value such that we don’t need extra space for dp[i][j].
for all i, 1 <= i < N
cost[i][0] = cost[i-1][0] + cost[i][0];
for all j, 1 <= j < N
cost[0][j] = cost[0][j-1] + cost[0][j];
otherwise
cost[i][j] = max(cost[i-1][j], cost[i][j-1]) + cost[i][j];
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double maxAverageOfPath(vector<vector<int>>cost)
{
int N = cost.size();
for (int i = 1; i < N; i++)
cost[i][0] = cost[i][0] + cost[i - 1][0];
for (int j = 1; j < N; j++)
cost[0][j] = cost[0][j - 1] + cost[0][j];
for (int i = 1; i < N; i++)
for (int j = 1; j <= N; j++)
cost[i][j] = max(cost[i - 1][j], cost[i][j - 1]) + cost[i][j];
return (double)cost[N - 1][N - 1] / (2 * N - 1);
}
int main()
{
vector<vector<int>> cost = {{1, 2, 3},
{6, 5, 4},
{7, 3, 9}
};
cout << maxAverageOfPath(cost);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double maxAverageOfPath(int[][] cost)
{
int N = cost.length;
for (int i = 1; i < N; i++)
cost[i][0] = cost[i][0] + cost[i - 1][0];
for (int j = 1; j < N; j++)
cost[0][j] = cost[0][j - 1] + cost[0][j];
for (int i = 1; i < N; i++)
for (int j = 1; j < N; j++)
cost[i][j] = Math.max(cost[i - 1][j],
cost[i][j - 1])
+ cost[i][j];
return (double)cost[N - 1][N - 1] / (2 * N - 1);
}
public static void main(String[] args)
{
int[][] cost
= { { 1, 2, 3 }, { 6, 5, 4 }, { 7, 3, 9 } };
System.out.println(maxAverageOfPath(cost));
}
}
|
Python3
from typing import List
def maxAverageOfPath(cost: List[List[int]]) -> float:
N = len(cost)
for i in range(1, N):
cost[i][0] = cost[i][0] + cost[i - 1][0]
for j in range(1, N):
cost[0][j] = cost[0][j - 1] + cost[0][j]
for i in range(1, N):
for j in range(1, N):
cost[i][j] = max(cost[i - 1][j], cost[i][j - 1]) + cost[i][j]
return cost[N - 1][N - 1] / (2 * N - 1)
def main():
cost = [[1, 2, 3],
[6, 5, 4],
[7, 3, 9]]
print(maxAverageOfPath(cost))
if __name__ == '__main__':
main()
|
C#
using System;
class GFG {
static double maxAverageOfPath(int[, ] cost)
{
int N = cost.GetLength(0);
for (int i = 1; i < N; i++)
cost[i, 0] = cost[i, 0] + cost[i - 1, 0];
for (int j = 1; j < N; j++)
cost[0, j] = cost[0, j - 1] + cost[0, j];
for (int i = 1; i < N; i++)
for (int j = 1; j < N; j++)
cost[i, j] = Math.Max(cost[i - 1, j],
cost[i, j - 1])
+ cost[i, j];
return (double)cost[N - 1, N - 1] / (2 * N - 1);
}
static void Main(string[] args)
{
int[, ] cost
= { { 1, 2, 3 }, { 6, 5, 4 }, { 7, 3, 9 } };
Console.WriteLine(maxAverageOfPath(cost));
}
}
|
Javascript
function maxAverageOfPath(cost)
{
let N = cost.length;
for (let i = 1; i < N; i++)
cost[i][0] = cost[i][0] + cost[i - 1][0];
for (let j = 1; j < N; j++)
cost[0][j] = cost[0][j - 1] + cost[0][j];
for (let i = 1; i < N; i++)
for (let j = 1; j <= N; j++)
cost[i][j] = Math.max(cost[i - 1][j], cost[i][j - 1]) + cost[i][j];
return (cost[N - 1][N - 1]) / (2.0 * N - 1);
}
let cost = [[1, 2, 3],
[6, 5, 4],
[7, 3, 9]];
console.log(maxAverageOfPath(cost))
|
Time Complexity: O(N*N)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Last Updated :
30 Dec, 2022
Like Article
Save Article