给定整数N 、 M 、 R和C ,其中N和M表示矩阵中的行数和列数, (R, C)表示该矩阵中的一个单元格,任务是找到离矩阵最远的单元格的距离细胞(R,C) 。
注意:矩阵一次只能水平或垂直遍历。
例子:
Input: N = 15, M = 12, R = 1, C = 6
Output: 20
Explanation: The maximum distance calculated from all the four corners are 20, 5, 19, 6. Therefore, 20 is the required answer.
Input: N = 15, M = 12, R = 2, C = 4
Output: 21
朴素的方法:最简单的方法是遍历矩阵并计算矩阵的每个单元格与给定单元格(R,C)的距离并保持所有距离的最大值。
时间复杂度: O(N * M)
辅助空间: O(1)
有效的方法:为了优化上述方法,观察到距矩阵中任何单元最远的单元将是四个最角落单元之一,即(1, 1), (1, M), (N, 1), (N, M) 。
请按照以下步骤解决问题:
- 初始化d1 , d2 , d3和d4分别等于N + M – R – C , R + C – 2 , N – R + C – 1和M – C + R – 1 。
- 打印d1 、 d2 、 d3和d4 中的最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the farthest
// cell distance from the given cell
void farthestCellDistance(int N, int M,
int R, int C)
{
// Distance from all the
// cornermost cells
// From cell(N, M)
int d1 = N + M - R - C;
// From Cell(1, 1)
int d2 = R + C - 2;
// From cell(N, 1)
int d3 = N - R + C - 1;
// From cell(1, M)
int d4 = M - C + R - 1;
// Finding out maximum
int maxDistance = max(d1,
max(d2,
max(d3, d4)));
// Print the answer
cout << maxDistance;
}
// Driver Code
int main()
{
int N = 15, M = 12, R = 1, C = 6;
farthestCellDistance(N, M, R, C);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the farthest
// cell distance from the given cell
static void farthestCellDistance(int N, int M,
int R, int C)
{
// Distance from all the
// cornermost cells
// From cell(N, M)
int d1 = N + M - R - C;
// From Cell(1, 1)
int d2 = R + C - 2;
// From cell(N, 1)
int d3 = N - R + C - 1;
// From cell(1, M)
int d4 = M - C + R - 1;
// Finding out maximum
int maxDistance = Math.max(d1, Math.max(
d2, Math.max(d3, d4)));
// Print the answer
System.out.println(maxDistance);
}
// Driver Code
public static void main(String[] args)
{
int N = 15, M = 12, R = 1, C = 6;
farthestCellDistance(N, M, R, C);
}
}
// This code is contributed by Dharanendra L V
Python3
# Python program for the above approach
# Function to find the farthest
# cell distance from the given cell
def farthestCellDistance(N, M, R, C):
# Distance from all the
# cornermost cells
# From cell(N, M)
d1 = N + M - R - C;
# From Cell(1, 1)
d2 = R + C - 2;
# From cell(N, 1)
d3 = N - R + C - 1;
# From cell(1, M)
d4 = M - C + R - 1;
# Finding out maximum
maxDistance = max(d1, max(d2, max(d3, d4)));
# Prthe answer
print(maxDistance);
# Driver Code
if __name__ == '__main__':
N = 15;
M = 12;
R = 1;
C = 6;
farthestCellDistance(N, M, R, C);
# This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the farthest
// cell distance from the given cell
static void farthestCellDistance(int N, int M,
int R, int C)
{
// Distance from all the
// cornermost cells
// From cell(N, M)
int d1 = N + M - R - C;
// From Cell(1, 1)
int d2 = R + C - 2;
// From cell(N, 1)
int d3 = N - R + C - 1;
// From cell(1, M)
int d4 = M - C + R - 1;
// Finding out maximum
int maxDistance = Math.Max(d1, Math.Max(
d2, Math.Max(d3, d4)));
// Print the answer
Console.WriteLine(maxDistance);
}
// Driver Code
static public void Main()
{
int N = 15, M = 12, R = 1, C = 6;
farthestCellDistance(N, M, R, C);
}
}
// This code is contributed by Dharanendra L V
Javascript
输出:
20
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。