📌  相关文章
📜  检查矩阵的任何行是否可以转换为目标行中存在的元素

📅  最后修改于: 2022-05-13 01:56:10.372000             🧑  作者: Mango

检查矩阵的任何行是否可以转换为目标行中存在的元素

给定一个维度为N*M的矩阵mat[][]和一个由M个整数组成的数组target[] ,任务是通过选择任意两行矩阵来检查是否可以使矩阵的任何行等于数组target[]矩阵并将任何行的每个元素更新为两个所选行的相应索引处的最大元素。如果可以这样做,请打印Yes 。否则,打印No

例子:

方法:给定的问题可以通过使用贪心方法来解决,想法是创建一个辅助数组comp[]并遍历矩阵的每一行,如果行的元素小于或等于目标数组中的相应元素然后对 row 和comp的每个元素执行 Math.max() 操作。可以按照以下步骤进行:

  • 初始化一个数组,比如大小为Mcomp[] ,每个元素都等于INT_MIN
  • 遍历矩阵mat[][]并为每一行检查该行中的所有值是否小于或等于目标数组中的相应值。如果发现为真,则通过将 comp[ ]中存在的每个元素与行中的相应元素取最大值来更新comp[]数组。
  • 经过上述步骤,如果数组comp[]与数组target[]相同,则打印Yes 。否则,打印No

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if target array
// can be formed from any row in trio
// with the given operations
int checkPossibleOrNot(vector >& mat,
                       vector& target)
{
 
    // Initialize comp[] of size M
    vector comp(mat[0].size(), INT_MIN);
 
    // Traverse the matrix mat[]
    for (auto val : mat) {
 
        // Check if all values in a row
        // is at most the corresponding
        // values in target[] array
        if (val[0] <= target[0] && val[1] <= target[1]
            && val[2] <= target[2])
 
            // Update the comp[]
            comp = { max(comp[0], val[0]),
                     max(comp[1], val[1]),
                     max(comp[2], val[2]) };
    }
 
    // Return the possibility
    return (comp == target);
}
 
// Driver Code
int main()
{
    vector > mat
        = { { 2, 5, 3 }, { 2, 8, 4 }, { 1, 5, 7 } };
    vector target = { 2, 5, 7 };
 
    string ans
        = checkPossibleOrNot(mat, target) ? "Yes" : "NO";
    cout << ans;
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to check if target array
    // can be formed from any row in trio
    // with the given operations
    public static boolean checkPossibleOrNot(
        int[][] mat, int[] target)
    {
 
        // Initialize comp[] of size M
        int[] comp = new int[mat[0].length];
 
        // Fill the array with minimum
        // value of integer
        Arrays.fill(comp, Integer.MIN_VALUE);
 
        // Traverse the matrix mat[]
        for (int[] val : mat) {
 
            // Check if all values in a row
            // is at most the corresponding
            // values in target[] array
            if (val[0] <= target[0]
                && val[1] <= target[1]
                && val[2] <= target[2])
 
                // Update the comp[]
                comp = new int[] { Math.max(
                                       comp[0], val[0]),
                                   Math.max(
                                       comp[1], val[1]),
                                   Math.max(
                                       comp[2], val[2]) };
        }
 
        // Return the possibility
        return Arrays.equals(comp, target);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[][] mat
            = { { 2, 5, 3 }, { 2, 8, 4 }, { 1, 5, 7 } };
        int[] target = { 2, 5, 7 };
 
        String ans = checkPossibleOrNot(
                         mat, target)
                         ? "Yes"
                         : "NO";
        System.out.println(ans);
    }
}


Python3
# Python3 program for the above approach
import sys
 
# Function to check if target array
# can be formed from any row in trio
# with the given operations
def checkPossibleOrNot(mat, target) :
 
    comp = [] ;
    INT_MIN = - (sys.maxsize - 1)
     
    # Initialize comp[] of size M
    for i in range(len(mat[0])) :
        comp.append(INT_MIN)
 
    # Traverse the matrix mat[]
    for val in mat :
 
        # Check if all values in a row
        # is at most the corresponding
        # values in target[] array
        if (val[0] <= target[0] and val[1] <= target[1] and val[2] <= target[2]) :
 
            # Update the comp[]
            comp = [ max(comp[0], val[0]), max(comp[1], val[1]), max(comp[2], val[2]) ];
     
    if comp == target :
         
        # Return the possibility
        return True
    else :
         
        # Return the possibility
        return False
 
# Driver Code
if __name__ == "__main__" :
     
    mat = [ [ 2, 5, 3 ], [ 2, 8, 4 ], [ 1, 5, 7 ] ];
    target = [ 2, 5, 7 ];
 
    ans = checkPossibleOrNot(mat, target)
     
    if ans:
        print("Yes")
    else :
        print("NO")
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
using System.Linq;
 
public class GFG {
 
    // Function to check if target array
    // can be formed from any row in trio
    // with the given operations
    public static bool checkPossibleOrNot(
        int [,]mat, int[] target)
    {
 
        // Initialize comp[] of size M
        int[] comp = new int[mat.GetLength(0)];
 
        // Fill the array with minimum
        // value of integer
        for (int i = 0; i < mat.GetLength(0); i++)
            comp[i] = int.MinValue;
             
        // Traverse the matrix mat[]
        for (int i = 0; i < mat.GetLength(0); i++){
             
            int []val = {mat[i, 0], mat[i, 1], mat[i, 2]};
 
            // Check if all values in a row
            // is at most the corresponding
            // values in target[] array
            if (val[0] <= target[0]
                && val[1] <= target[1]
                && val[2] <= target[2])
 
                // Update the comp[]
                comp = new int[] { Math.Max(
                                    comp[0], val[0]),
                                Math.Max(
                                    comp[1], val[1]),
                                Math.Max(
                                    comp[2], val[2]) };
        }
 
        // Return the possibility
        return comp.SequenceEqual(target);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[,] mat = { { 2, 5, 3 }, { 2, 8, 4 }, { 1, 5, 7 } };
        int[] target = { 2, 5, 7 };
 
        string ans = checkPossibleOrNot(
                        mat, target)
                        ? "Yes"
                        : "NO";
        Console.WriteLine(ans);
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
Yes

时间复杂度: O(N*M)
辅助空间: O(1)