给定矩阵中相邻行中相同列元素的所有绝对差的最小和
给定一个具有N行和M列的矩阵mat[][] ,任务是找到两个相邻行之间的最小距离,其中两行之间的距离定义为存在于同一列的两个元素之间的所有绝对差的总和在两排。
例子:
Input: mat[][] = {{1, 4, 7, 10}, {2, 5, 8, 11}, {6, 9, 3, 12}}
Output: 4
Explanation: The distance between the first two rows can be calculated as (2-1) + (5-4) + (8-7) + (11-10) = 4. Similarly, the distance between the 2nd and 3rd row can be calculated as (6-2) + (9-5) + (8-3) + (12-11) = 14. Hence, the minimum distance among all adjacent rows is 4.
Input: mat[][] = {{1, 25, 81}, {2, 36, 100}, {9, 49, 50}, {16, 64, 25}}
Output : 31
方法:给定的问题是一个基于实现的问题,可以通过逐行遍历矩阵并计算所有相邻行对的距离来解决。保持变量中所有计算距离的最小值,这是所需的答案。
以下是上述方法的实现:
C++
// C++ program of the above approach.
#include
using namespace std;
// Function to find minimum distance
// between two adjacent rows in mat
int calcDist(int N, int M, vector > mat)
{
// Stores the required value
int ans = INT_MAX;
// Loop to traverse all the
// pair of rows in mat[][]
for (int i = 0; i < N - 1; i++) {
// Stores the distance
int dist = 0;
// Loop to calculate
// the distance
for (int j = 0; j < M; j++) {
dist += abs(mat[i][j] - mat[i + 1][j]);
}
// Update ans
ans = min(ans, dist);
}
// Return Answer
return ans;
}
// C++ program of the above approach
int main()
{
vector > mat = { { 1, 4, 7, 10 },
{ 2, 5, 8, 11 },
{ 6, 9, 3, 2 } };
cout << calcDist(mat.size(), mat[0].size(), mat);
return 0;
}
Java
// JAVA program of the above approach.
import java.util.*;
class GFG
{
// Function to find minimum distance
// between two adjacent rows in mat
public static int
calcDist(int N, int M,
ArrayList > mat)
{
// Stores the required value
int ans = Integer.MAX_VALUE;
// Loop to traverse all the
// pair of rows in mat[][]
for (int i = 0; i < N - 1; i++)
{
// Stores the distance
int dist = 0;
// Loop to calculate
// the distance
for (int j = 0; j < M; j++) {
dist += Math.abs(mat.get(i).get(j)
- mat.get(i + 1).get(j));
}
// Update ans
ans = Math.min(ans, dist);
}
// Return Answer
return ans;
}
// JAVA program of the above approach
public static void main(String[] args)
{
ArrayList > mat
= new ArrayList >();
ArrayList temp1 = new ArrayList(
Arrays.asList(1, 4, 7, 10));
ArrayList temp2 = new ArrayList(
Arrays.asList(2, 5, 8, 11));
ArrayList temp3 = new ArrayList(
Arrays.asList(6, 9, 3, 2));
mat.add(temp1);
mat.add(temp2);
mat.add(temp3);
System.out.print(
calcDist(mat.size(), mat.get(0).size(), mat));
}
}
// This code is contributed by Taranpreet
Python3
# python3 program of the above approach.
INT_MAX = 2147483647
# Function to find minimum distance
# between two adjacent rows in mat
def calcDist(N, M, mat):
# Stores the required value
ans = INT_MAX
# Loop to traverse all the
# pair of rows in mat[][]
for i in range(0, N - 1):
# Stores the distance
dist = 0
# Loop to calculate
# the distance
for j in range(0, M):
dist += abs(mat[i][j] - mat[i + 1][j])
# Update ans
ans = min(ans, dist)
# Return Answer
return ans
if __name__ == "__main__":
mat = [[1, 4, 7, 10],
[2, 5, 8, 11],
[6, 9, 3, 2]]
print(calcDist(len(mat), len(mat[0]), mat))
# This code is contributed by rakeshsahni
C#
// C# program of the above approach.
using System;
class GFG
{
// Function to find minimum distance
// between two adjacent rows in mat
static int calcDist(int N, int M, int[, ] mat)
{
// Stores the required value
int ans = Int32.MaxValue;
// Loop to traverse all the
// pair of rows in mat[][]
for (int i = 0; i < N - 1; i++) {
// Stores the distance
int dist = 0;
// Loop to calculate
// the distance
for (int j = 0; j < M; j++) {
dist += Math.Abs(mat[i, j] - mat[i + 1, j]);
}
// Update ans
ans = Math.Min(ans, dist);
}
// Return Answer
return ans;
}
// Criver code
public static void Main()
{
int[, ] mat = { { 1, 4, 7, 10 },
{ 2, 5, 8, 11 },
{ 6, 9, 3, 2 } };
Console.Write(calcDist(mat.GetLength(0),
mat.GetLength(1), mat));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
4
时间复杂度: O(N*M)
辅助空间: O(1)