📜  查找位置元素的数量

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

查找位置元素的数量

给定一个整数矩阵,任务是找出位置元素的数量。位置元素是在行或列中为最小值或最大值的元素。

例子:

来源:高盛采访集
想法是存储每行和每列的最大值和最小值,然后检查所需的条件。



下面是上述方法的实现。

C++
// CPP program to find positional elements in
// a matrix.
#include 
using namespace std;
 
const int MAX = 100;
 
int countPositional(int a[][MAX], int m, int n)
{
    // rwomax[i] is going to store maximum of
    // i-th row and other arrays have similar
    // meaning
    int rowmax[m], rowmin[m];
    int colmax[n], colmin[n];
 
    // Find rminn and rmaxx for every row
    for (int i = 0; i < m; i++) {
        int rminn = INT_MAX;
        int rmaxx = INT_MIN;
        for (int j = 0; j < n; j++) {
            if (a[i][j] > rmaxx)
                rmaxx = a[i][j];
            if (a[i][j] < rminn)
                rminn = a[i][j];
        }
        rowmax[i] = rmaxx;
        rowmin[i] = rminn;
    }
 
    // Find cminn and cmaxx for every column
    for (int j = 0; j < n; j++) {
        int cminn = INT_MAX;
        int cmaxx = INT_MIN;
        for (int i = 0; i < m; i++) {
            if (a[i][j] > cmaxx)
                cmaxx = a[i][j];
            if (a[i][j] < cminn)
                cminn = a[i][j];
        }
 
        colmax[j] = cmaxx;
        colmin[j] = cminn;
    }
 
    // Check for optimal element
    int count = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if ((a[i][j] == rowmax[i])
                || (a[i][j] == rowmin[i])
                || (a[i][j] == colmax[j])
                || (a[i][j] == colmin[j])) {
                count++;
            }
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int a[][MAX] = { { 1, 3, 4 },
                     { 5, 2, 9 },
                     { 8, 7, 6 } };
    int m = 3, n = 3;
    cout << countPositional(a, m, n);
    return 0;
}


Java
// Java program to find positional elements in
// a matrix.
class GfG {
 
    static int MAX = 100;
 
    static int countPositional(int a[][], int m, int n)
    {
        // rwomax[i] is going to store maximum of
        // i-th row and other arrays have similar
        // meaning
        int rowmax[] = new int[m];
        int rowmin[] = new int[m];
        int colmax[] = new int[n];
        int colmin[] = new int[n];
 
        // Find rminn and rmaxx for every row
        for (int i = 0; i < m; i++) {
            int rminn = Integer.MAX_VALUE;
            int rmaxx = Integer.MIN_VALUE;
            for (int j = 0; j < n; j++) {
                if (a[i][j] > rmaxx)
                    rmaxx = a[i][j];
                if (a[i][j] < rminn)
                    rminn = a[i][j];
            }
            rowmax[i] = rmaxx;
            rowmin[i] = rminn;
        }
 
        // Find cminn and cmaxx for every column
        for (int j = 0; j < n; j++) {
            int cminn = Integer.MAX_VALUE;
            int cmaxx = Integer.MIN_VALUE;
            for (int i = 0; i < m; i++) {
                if (a[i][j] > cmaxx)
                    cmaxx = a[i][j];
                if (a[i][j] < cminn)
                    cminn = a[i][j];
            }
 
            colmax[j] = cmaxx;
            colmin[j] = cminn;
        }
 
        // Check for optimal element
        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ((a[i][j] == rowmax[i])
                    || (a[i][j] == rowmin[i])
                    || (a[i][j] == colmax[j])
                    || (a[i][j] == colmin[j])) {
                    count++;
                }
            }
        }
 
        return count;
    }
 
    public static void main(String[] args)
    {
        int a[][] = new int[][] { { 1, 3, 4 }, { 5, 2, 9 }, { 8, 7, 6 } };
        int m = 3, n = 3;
        System.out.println(countPositional(a, m, n));
    }
}


Python3
# Python3 program to find positional elements in a matrix.
import sys
 
MAX = 100
 
def countPositional(a, m, n):
 
    # rwomax[i] is going to store maximum of
    # i-th row and other arrays have similar
    # meaning
    rowmax = [0] * m
    rowmin = [0] * m
    colmax = [0] * n
    colmin = [0] * n
 
    # Find rminn and rmaxx for every row
    for i in range(m) :
        rminn = sys.maxsize
        rmaxx = -sys.maxsize
        for j in range(n) :
            if (a[i][j] > rmaxx) :
                rmaxx = a[i][j]
            if (a[i][j] < rminn) :
                rminn = a[i][j]
     
        rowmax[i] = rmaxx
        rowmin[i] = rminn
 
    # Find cminn and cmaxx for every column
    for j in range(n) :
        cminn = sys.maxsize
        cmaxx = -sys.maxsize
        for i in range(m) :
            if (a[i][j] > cmaxx) :
                cmaxx = a[i][j]
            if (a[i][j] < cminn) :
                cminn = a[i][j]
 
        colmax[j] = cmaxx
        colmin[j] = cminn
 
    # Check for optimal element
    count = 0
    for i in range(m) :
        for j in range(n) :
            if ((a[i][j] == rowmax[i]) or (a[i][j] == rowmin[i])
                or (a[i][j] == colmax[j])
                or (a[i][j] == colmin[j])) :
                count += 1
 
    return count
 
# Driver code
a = [ [ 1, 3, 4 ], [ 5, 2, 9 ], [ 8, 7, 6 ] ]
m, n = 3, 3
print(countPositional(a, m, n))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to find positional elements in
using System;
 
class GFG {
 
    static int countPositional(int[, ] a, int m, int n)
    {
        // rwomax[i] is going to store maximum of
        // i-th row and other arrays have similar
        // meaning
        int[] rowmax = new int[m];
        int[] rowmin = new int[m];
        int[] colmax = new int[n];
        int[] colmin = new int[n];
 
        // Find rminn and rmaxx for every row
        for (int i = 0; i < m; i++) {
            int rminn = int.MaxValue;
            int rmaxx = int.MinValue;
            for (int j = 0; j < n; j++) {
                if (a[i, j] > rmaxx)
                    rmaxx = a[i, j];
                if (a[i, j] < rminn)
                    rminn = a[i, j];
            }
            rowmax[i] = rmaxx;
            rowmin[i] = rminn;
        }
 
        // Find cminn and cmaxx for every column
        for (int j = 0; j < n; j++) {
            int cminn = int.MaxValue;
            int cmaxx = int.MinValue;
            for (int i = 0; i < m; i++) {
                if (a[i, j] > cmaxx)
                    cmaxx = a[i, j];
                if (a[i, j] < cminn)
                    cminn = a[i, j];
            }
 
            colmax[j] = cmaxx;
            colmin[j] = cminn;
        }
 
        // Check for optimal element
        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ((a[i, j] == rowmax[i])
                    || (a[i, j] == rowmin[i])
                    || (a[i, j] == colmax[j])
                    || (a[i, j] == colmin[j])) {
                    count++;
                }
            }
        }
 
        return count;
    }
 
    // Driver Code
    static public void Main()
    {
        int[, ] a = new int[, ] { { 1, 3, 4 }, { 5, 2, 9 }, { 8, 7, 6 } };
        int m = 3, n = 3;
        Console.WriteLine(countPositional(a, m, n));
    }
}
 
// This code is contributed by Tushil.


Javascript


输出:
7

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程