通过翻转任何相邻对的符号来最大化矩阵和
给定一个维度为N*N的矩阵mat[] ,任务是通过任意次数翻转相邻元素的符号来找到矩阵元素的最大和。
例子:
Input: mat[][] = [[2, -2], [-2, 2]]
OutputL: 8
Explanation:
Follow the steps below to find the maximum sum of matrix as:
- Flipping the sign of adjacent element (arr[0][0], arr[0][1]) modifies the matrix to mat[][] = {{-2, 2}, {-2, 2}}.
- Flipping the sign of adjacent element (arr[0][0], arr[1][0]) modifies the matrix to mat[][] = {{2, 2}, {2, 2}}.
Now, the sum of matrix elements is 8, which is the maximum among all possible flipping of adjacent matrix elements.
Input: mat[][] = [[1, 2, 3], [-1, -2, -3], [1, 2, 3]]
Output: 16
方法:给定问题可以通过观察以下事实来解决:如果矩阵中有偶数个负数,则可以将所有这些元素转换为正元素以获得最大和。否则,除一个负元素外,所有矩阵元素都可以翻转。请按照以下步骤解决问题:
- 求所有矩阵元素的绝对值之和,并将其存储在变量S中。
- 找到具有最小绝对值的矩阵元素并将其存储在变量中,例如minElement 。
- 如果给定矩阵mat[][]中负元素的计数是偶数,则将最大和打印为S 。否则,通过排除总和中的最小元素,将(S – 2*minElement)的值打印为结果总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// matrix element after flipping the
// signs of adjacent matrix elements
int maxSum(vector >& matrix)
{
// Initialize row and column
int r = matrix.size();
int c = matrix[0].size();
// Stores the sum of absolute
// matrix element
int sum = 0;
// Find the minimum absolute value
// in the matrix
int mini = INT_MAX;
// Store count of negatives
int count = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
int k = matrix[i][j];
// Find the smallest absolute
// value in the matrix
mini = min(mini, abs(k));
// Increment the count of
// negative numbers
if (k < 0)
count++;
// Find the absolute sum
sum += abs(k);
}
}
// If the count of negatives is
// even then print the sum
if (count % 2 == 0) {
return sum;
}
// Subtract minimum absolute
// element
else {
return (sum - 2 * mini);
}
}
// Driver Code
int main()
{
vector > matrix
= { { 2, -2 }, { -2, 2 } };
cout << maxSum(matrix);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the maximum sum of
// matrix element after flipping the
// signs of adjacent matrix elements
static int maxSum(int[][] matrix)
{
// Initialize row and column
int r = matrix.length;
int c = matrix[0].length;
// Stores the sum of absolute
// matrix element
int sum = 0;
// Find the minimum absolute value
// in the matrix
int mini = Integer.MAX_VALUE;
// Store count of negatives
int count = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
int k = matrix[i][j];
// Find the smallest absolute
// value in the matrix
mini = Math.min(mini, Math.abs(k));
// Increment the count of
// negative numbers
if (k < 0)
count++;
// Find the absolute sum
sum += Math.abs(k);
}
}
// If the count of negatives is
// even then print the sum
if (count % 2 == 0) {
return sum;
}
// Subtract minimum absolute
// element
else {
return (sum - 2 * mini);
}
}
// Driver Code
public static void main(String[] args)
{
int[][] matrix = { { 2, -2 }, { -2, 2 } };
System.out.print(maxSum(matrix));
}
}
// This code is contributed by subham348.
Python3
# Python 3 program for the above approach
import sys
# Function to find the maximum sum of
# matrix element after flipping the
# signs of adjacent matrix elements
def maxSum(matrix):
# Initialize row and column
r = len(matrix)
c = len(matrix[0])
# Stores the sum of absolute
# matrix element
sum = 0
# Find the minimum absolute value
# in the matrix
mini = sys.maxsize
# Store count of negatives
count = 0
for i in range(r):
for j in range(c):
k = matrix[i][j]
# Find the smallest absolute
# value in the matrix
mini = min(mini, abs(k))
# Increment the count of
# negative numbers
if (k < 0):
count += 1
# Find the absolute sum
sum += abs(k)
# If the count of negatives is
# even then print the sum
if (count % 2 == 0):
return sum
# Subtract minimum absolute
# element
else:
return (sum - 2 * mini)
# Driver Code
if __name__ == '__main__':
matrix = [[2, -2],[-2, 2]]
print(maxSum(matrix))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the maximum sum of
// matrix element after flipping the
// signs of adjacent matrix elements
static int maxSum(int[,] matrix)
{
// Initialize row and column
int r = matrix.GetLength(0);
int c = matrix.GetLength(1);
// Stores the sum of absolute
// matrix element
int sum = 0;
// Find the minimum absolute value
// in the matrix
int mini = int.MaxValue;
// Store count of negatives
int count = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
int k = matrix[i,j];
// Find the smallest absolute
// value in the matrix
mini = Math.Min(mini, Math.Abs(k));
// Increment the count of
// negative numbers
if (k < 0)
count++;
// Find the absolute sum
sum += Math.Abs(k);
}
}
// If the count of negatives is
// even then print the sum
if (count % 2 == 0) {
return sum;
}
// Subtract minimum absolute
// element
else {
return (sum - 2 * mini);
}
}
// Driver Code
public static void Main(string[] args)
{
int[,] matrix = { { 2, -2 }, { -2, 2 } };
Console.Write(maxSum(matrix));
}
}
// This code is contributed by AnkThon
Javascript
输出:
8
时间复杂度: O(N 2 )
辅助空间: O(1)