#include <iostream>
#include <algorithm>
const int Total_rows = 50;
const int Total_cols = 50;
int max_power_finder(int power_mat[][Total_cols], int M, int N) {
int max_power = 0;
if (M == 1) {
max_power = *std::max_element(power_mat[0], power_mat[0] + N);
} else {
// Initialize the DP matrix with the first column of power_mat
int temp_dpmat[Total_rows][Total_cols] = {0};
for (int i = 0; i < M; ++i) {
temp_dpmat[i][0] = power_mat[i][0];
}
// Fill the rest of the DP matrix
for (int j = 1; j < N; ++j) {
for (int i = 0; i < M; ++i) {
// Calculate the maximum power for the current cell
int max_power_from_prev_row = 0;
for (int k = 0; k < M; ++k) {
if (k != i) {
max_power_from_prev_row = std::max(max_power_from_prev_row, temp_dpmat[k][j - 1]);
}
}
temp_dpmat[i][j] = power_mat[i][j] + max_power_from_prev_row;
}
}
// Find the maximum power team
for (int i = 0; i < M; ++i) {
max_power = std::max(max_power, temp_dpmat[i][N - 1]);
}
}
return max_power;
}
I have to calculate the max sum of the 2d array such that no two consequtive selections should be from the same row and column and the slected values’ column should be greater than the previosly selected value.
New contributor
M_A0803 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.