给定一个矩阵m [] [] ,任务是检查给定的矩阵是否是Bitonic的。如果给定的矩阵是Bitonic ,则打印YES 。否则,打印NO 。
If all the rows and the columns of the given matrix have elements in one of the following orders:
- Strictly increasing
- Strictly decreasing
- Strictly increasing followed by strictly decreasing
Then the given matrix is said to be a Bitonic Matrix
例子:
Input: m[][] = {{1, 2, 3}, {4, 5, 6}, {2, 3, 4}}
Output: YES
Explanation:
All the columns of the given matrix {1, 4, 2}, {2, 5, 3}, {3, 6, 4} forms an increasing followed by decreasing sequence
All the rows of the given matrix have an increasing sequence.
Therefore, the matrix is Bitonic.
Input: m[][] = {{1, 2, 3}, {4, 5, 6}, {2, 5, 4}}
Output: NO
Explanation:
Since the column {2, 5, 5} does not satisfy any of the three conditions, the given matrix is not Bitonic.
方法:
请按照以下步骤解决问题:
- 逐一检查矩阵的每一行的元素(如果它们未形成Bitonic序列)。如果发现任何行不是Bitonic,请打印NO 。
- 同样,如果每一列的元素是否形成双音序列,则一一检查。如果发现任何行不是Bitonic,请打印NO 。
- 如果发现所有行和列都是Bitonic ,则打印YES
下面是上述方法的实现:
C++
// C++ Program to check if a
// matrix is Bitonic or not
#include
using namespace std;
const int N = 3, M = 3;
// Function to check if an
// array is Bitonic or not
bool checkBitonic(int arr[], int n)
{
int i, j, f = 0;
// Check for increasing sequence
for (i = 1; i < n; i++) {
if (arr[i] > arr[i - 1])
continue;
if (arr[i] == arr[i - 1])
return false;
else {
f = 1;
break;
}
}
if (i == n)
return true;
// Check for decreasing sequence
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[j - 1])
continue;
if (arr[i] == arr[i - 1])
return false;
else {
if (f == 1)
return false;
}
}
return true;
}
// Function to check whether given
// matrix is bitonic or not
void check(int arr[N][M])
{
int f = 0;
// Check row-wise
for (int i = 0; i < N; i++) {
if (!checkBitonic(arr[i], M)) {
cout << "NO" << endl;
return;
}
}
// Check column wise
for (int i = 0; i < N; i++) {
// Generate an array
// consisting of elements
// of the current column
int temp[N];
for (int j = 0; j < N; j++) {
temp[j] = arr[j][i];
}
if (!checkBitonic(temp, N)) {
cout << "NO" << endl;
return;
}
}
cout << "YES";
}
// Driver Code
int main()
{
int m[N][M] = { { 1, 2, 3 },
{ 3, 4, 5 },
{ 2, 6, 4 } };
check(m);
return 0;
}
Java
// Java program to check if a
// matrix is Bitonic or not
class GFG{
final static int N = 3, M = 3;
// Function to check if an
// array is Bitonic or not
static boolean checkBitonic(int arr[], int n)
{
int i, j, f = 0;
// Check for increasing sequence
for(i = 1; i < n; i++)
{
if (arr[i] > arr[i - 1])
continue;
if (arr[i] == arr[i - 1])
return false;
else
{
f = 1;
break;
}
}
if (i == n)
return true;
// Check for decreasing sequence
for(j = i + 1; j < n; j++)
{
if (arr[j] < arr[j - 1])
continue;
if (arr[i] == arr[i - 1])
return false;
else
{
if (f == 1)
return false;
}
}
return true;
}
// Function to check whether given
// matrix is bitonic or not
static void check(int arr[][])
{
int f = 0;
// Check row-wise
for(int i = 0; i < N; i++)
{
if (!checkBitonic(arr[i], M))
{
System.out.println("NO");
return;
}
}
// Check column wise
for(int i = 0; i < N; i++)
{
// Generate an array
// consisting of elements
// of the current column
int temp[] = new int[N];
for(int j = 0; j < N; j++)
{
temp[j] = arr[j][i];
}
if (!checkBitonic(temp, N))
{
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
// Driver Code
public static void main(String[] args)
{
int m[][] = { { 1, 2, 3 },
{ 3, 4, 5 },
{ 2, 6, 4 } };
check(m);
}
}
// This code is contributed by rutvik_56
Python3
# Python3 program to check if a
# matrix is Bitonic or not
N = 3
M = 3
# Function to check if an
# array is Bitonic or not
def checkBitonic(arr, n):
i, j, f = 0, 0, 0
# Check for increasing sequence
for i in range(1, n):
if (arr[i] > arr[i - 1]):
continue
if (arr[i] == arr[i - 1]):
return False
else:
f = 1
break
if (i == n):
return True
# Check for decreasing sequence
for j in range(i + 1, n):
if (arr[j] < arr[j - 1]):
continue
if (arr[i] == arr[i - 1]):
return False
else:
if (f == 1):
return False
return True
# Function to check whether given
# matrix is bitonic or not
def check(arr):
f = 0
# Check row wise
for i in range(N):
if (not checkBitonic(arr[i], M)):
print("NO")
return
# Check column wise
i = 0
for i in range(N):
# Generate an array consisting
# of elements of current column
temp = [0] * N
for j in range(N):
temp[j] = arr[j][i]
if (not checkBitonic(temp, N)):
print("NO")
return
print("YES")
# Driver Code
if __name__ == '__main__':
m = [ [ 1, 2, 3 ],
[ 3, 4, 5 ],
[ 2, 6, 4 ] ]
check(m)
# This code is contributed by himanshu77
C#
// C# program to check if a
// matrix is Bitonic or not
using System;
class GFG{
readonly static int N = 3, M = 3;
// Function to check if an
// array is Bitonic or not
static bool checkBitonic(int []arr, int n)
{
int i, j, f = 0;
// Check for increasing sequence
for(i = 1; i < n; i++)
{
if (arr[i] > arr[i - 1])
continue;
if (arr[i] == arr[i - 1])
return false;
else
{
f = 1;
break;
}
}
if (i == n)
return true;
// Check for decreasing sequence
for(j = i + 1; j < n; j++)
{
if (arr[j] < arr[j - 1])
continue;
if (arr[i] == arr[i - 1])
return false;
else
{
if (f == 1)
return false;
}
}
return true;
}
// Function to check whether given
// matrix is bitonic or not
static void check(int [,]arr)
{
int f = 0;
// Check row-wise
for(int i = 0; i < N; i++)
{
if (!checkBitonic(GetRow(arr, i), M))
{
Console.WriteLine("NO");
return;
}
}
// Check column wise
for(int i = 0; i < N; i++)
{
// Generate an array
// consisting of elements
// of the current column
int []temp = new int[N];
for(int j = 0; j < N; j++)
{
temp[j] = arr[j, i];
}
if (!checkBitonic(temp, N))
{
Console.WriteLine("NO");
return;
}
}
Console.WriteLine("YES");
}
public static int[] GetRow(int[,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int[rowLength];
for (var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
// Driver Code
public static void Main(String[] args)
{
int [,]m = { { 1, 2, 3 },
{ 3, 4, 5 },
{ 2, 6, 4 } };
check(m);
}
}
// This code is contributed by sapnasingh4991
YES
时间复杂度: O(N * M)
辅助空间: O(N)