📌  相关文章
📜  查找行中最小且列中最大的所有矩阵元素

📅  最后修改于: 2021-09-07 03:11:15             🧑  作者: Mango

给定一个大小为M * N的矩阵mat[][] ,任务是找到所有矩阵元素,它们在各自的行中最小,在各自的列中最大。如果不存在这样的元素,则打印-1

例子:

处理方法:按照以下步骤解决问题:

  1. 创建一个unordered_set并存储矩阵每一行的最小元素。
  2. 遍历矩阵并找到每列的最大元素。对于每一列,检查获得的最大值是否已经存在于 unordered_set 中。
  3. 如果发现是真的,请打印该数字。如果找不到这样的矩阵元素,则打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Functionto find all the matrix elements
// which are minimum in its row and maximum
// in its column
vector minmaxNumbers(vector >& matrix,
                          vector& res)
{
 
    // Initialize unordered set
    unordered_set set;
 
    // Traverse the matrix
    for (int i = 0; i < matrix.size(); i++) {
        int minr = INT_MAX;
        for (int j = 0; j < matrix[i].size(); j++) {
 
            // Update the minimum
            // element of current row
            minr = min(minr, matrix[i][j]);
        }
 
        // Insert the minimum
        // element of the row
        set.insert(minr);
    }
 
    for (int j = 0; j < matrix[0].size(); j++) {
        int maxc = INT_MIN;
 
        for (int i = 0; i < matrix.size(); i++) {
 
            // Update the maximum
            // element of current column
            maxc = max(maxc, matrix[i][j]);
        }
 
        // Checking if it is already present
        // in the unordered_set or not
        if (set.find(maxc) != set.end()) {
            res.push_back(maxc);
        }
    }
 
    return res;
}
 
// Driver Code
int main()
{
    vector > mat
        = { { 1, 10, 4 },
            { 9, 3, 8 },
            { 15, 16, 17 } };
 
    vector ans;
 
    // Function call
    minmaxNumbers(mat, ans);
 
    // If no such matrix
    // element is found
    if (ans.size() == 0)
        cout << "-1" << endl;
 
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Functionto find all the matrix elements
// which are minimum in its row and maximum
// in its column
public static VectorminmaxNumbers(int[][] matrix,
              Vector res)        
{
     
    // Initialize unordered set
    Set set = new HashSet();
   
    // Traverse the matrix
    for(int i = 0; i < matrix.length; i++)
    {
        int minr = Integer.MAX_VALUE;
        for(int j = 0; j < matrix[i].length; j++)
        {
             
            // Update the minimum
            // element of current row
            minr = Math.min(minr, matrix[i][j]);
        }
   
        // Insert the minimum
        // element of the row
        set.add(minr);
    }
   
    for(int j = 0; j < matrix[0].length; j++)
    {
        int maxc = Integer.MIN_VALUE;
        for(int i = 0; i < matrix.length; i++)
        {
             
            // Update the maximum
            // element of current column
            maxc = Math.max(maxc, matrix[i][j]);
        }
   
        // Checking if it is already present
        // in the unordered_set or not
        if (set.contains(maxc))
        {
            res.add(maxc);
        }
    }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int[][] mat = { { 1, 10, 4 }, 
                    { 9, 3, 8 }, 
                    { 15, 16, 17 } };
 
    Vector ans = new Vector();
   
    // Function call
    ans = minmaxNumbers(mat, ans);
   
    // If no such matrix
    // element is found
    if (ans.size() == 0)
        System.out.println("-1");
   
    for(int i = 0; i < ans.size(); i++)
        System.out.println(ans.get(i));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
import sys
 
# Functionto find all the matrix elements
# which are minimum in its row and maximum
# in its column
def minmaxNumbers(matrix, res):
     
    # Initialize unordered set
    s = set()
 
    # Traverse the matrix
    for i in range(0, len(matrix), 1):
        minr = sys.maxsize
        for j in range(0, len(matrix[i]), 1):
             
            # Update the minimum
            # element of current row
            minr = min(minr, matrix[i][j])
 
        # Insert the minimum
        # element of the row
        s.add(minr)
 
    for j in range(0, len(matrix[0]), 1):
        maxc = -sys.maxsize - 1
 
        for i in range(0, len(matrix), 1):
             
            # Update the maximum
            # element of current column
            maxc = max(maxc, matrix[i][j])
 
        # Checking if it is already present
        # in the unordered_set or not
        if (maxc in s):
            res.append(maxc)
 
    return res
 
# Driver Code
if __name__ == '__main__':
     
    mat = [ [ 1, 10, 4 ],
            [ 9, 3, 8 ],
            [ 15, 16, 17 ] ]
 
    ans = []
 
    # Function call
    minmaxNumbers(mat, ans)
 
    # If no such matrix
    # element is found
    if (len(ans) == 0):
        print("-1")
 
    for i in range(len(ans)):
        print(ans[i])
         
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Functionto find all
// the matrix elements
// which are minimum
// in its row and
//  maximum in its column
public static List minmaxNumbers(int[,] matrix,
                                      List res)        
{    
  // Initialize unordered set
  HashSet set = new HashSet();
 
  // Traverse the matrix
  for(int i = 0; i < matrix.GetLength(0); i++)
  {
    int minr = int.MaxValue;
    for(int j = 0; j < matrix.GetLength(1); j++)
    {
      // Update the minimum
      // element of current row
      minr = Math.Min(minr, matrix[i, j]);
    }
 
    // Insert the minimum
    // element of the row
    set.Add(minr);
  }
 
  for(int j = 0; j < matrix.GetLength(0); j++)
  {
    int maxc = int.MinValue;
    for(int i = 0; i < matrix.GetLength(1); i++)
    {
      // Update the maximum
      // element of current column
      maxc = Math.Max(maxc, matrix[i, j]);
    }
 
    // Checking if it is already present
    // in the unordered_set or not
    if (set.Contains(maxc))
    {
      res.Add(maxc);
    }
  }
  return res;
}
 
// Driver code
public static void Main(String[] args)
{
  int[,] mat = {{1, 10, 4}, 
                {9, 3, 8}, 
                {15, 16, 17}};
 
  List ans = new List();
 
  // Function call
  ans = minmaxNumbers(mat, ans);
 
  // If no such matrix
  // element is found
  if (ans.Count == 0)
    Console.WriteLine("-1");
 
  for(int i = 0; i < ans.Count; i++)
    Console.WriteLine(ans[i]);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
15

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live