Skip to content

1975. Maximum Matrix Sum #871

Answered by mah-shamim
mah-shamim asked this question in Q&A
Nov 24, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to minimize the absolute value of the negative contributions to the sum. Here's the plan:

  1. Count Negative Numbers: Track how many negative numbers are present in the matrix.
  2. Find Minimum Absolute Value: Determine the smallest absolute value in the matrix, which will help if the number of negatives is odd.
  3. Adjust for Odd Negatives: If the count of negative numbers is odd, the smallest absolute value cannot be flipped to positive, and this limits the maximum possible sum.

Let's implement this solution in PHP: 1975. Maximum Matrix Sum

<?php
/**
 * @param Integer[][] $matrix
 * @return Integer
 */
function maximumMatrixSum($matrix) {
    $n = count($matrix);
    $sum = 0;
    $minAbs = 

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Nov 24, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 24, 2024
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants