📌  相关文章
📜  检查是否存在具有最大X和Y坐标的点

📅  最后修改于: 2021-05-04 16:19:05             🧑  作者: Mango

给定一个二维数组arr [],它由(X,Y)形式的N个坐标组成,任务是从给定数组中找到一个坐标,以使该点的X坐标大于所有其他X坐标,并且该点的Y坐标大于所有其他Y坐标。如果不存在这样的点,则打印-1

例子:

天真的方法:最简单的方法是遍历数组,对于每个点,检查它是否为最大XY坐标。如果不存在这样的点,则打印-1 。否则,将点打印为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Initialize INF as inifnity
int INF = INT_MAX;
 
// Function to return the point having
// maximum X and Y coordinates
int* findMaxPoint(int arr[][2], int i, int n)
{
     
    // Base Case
    if (i == n)
    {
        arr[0][0] = INF;
        arr[0][1] = INF;
        return arr[0];
    }
 
    // Stores if valid point exists
    bool flag = true;
 
    // If point arr[i] is valid
    for(int j = 0; j < n; j++)
    {
         
        // Check for the same point
        if (j == i)
            continue;
 
        // Check for a valid point
        if (arr[j][0] >= arr[i][0] ||
            arr[j][1] >= arr[i][1])
        {
            flag = false;
            break;
        }
    }
 
    // If current point is the
    // required point
    if (flag)
        return arr[i];
 
    // Otherwise
    return findMaxPoint(arr, i + 1, n);
}
 
// Function to find the required point
void findMaxPoints(int arr[][2], int n)
{
     
    // Stores the point with maximum
    // X and Y-coordinates
    int ans[2];
    memcpy(ans, findMaxPoint(arr, 0, n),
           2 * sizeof(int));
 
    // If no required point exists
    if (ans[0] == INF)
    {
        cout << -1;
    }
    else
    {
        cout << "(" << ans[0] << " "
             << ans[1] << ")";
    }
}
 
// Driver Code
int main()
{
     
    // Given array of points
    int arr[][2] = { { 1, 2 }, { 2, 1 },
                     { 3, 4 }, { 4, 3 },
                     { 5, 5 } };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    findMaxPoints(arr, N);
     
    return 0;
}
 
// This code is contributed by subhammahato348


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Initialize INF as inifnity
    static int INF = Integer.MAX_VALUE;
 
    // Function to return the point having
    // maximum X and Y coordinates
    static int[] findMaxPoint(
        int arr[][], int i, int n)
    {
        // Base Case
        if (i == n)
            return new int[] { INF, INF };
 
        // Stores if valid point exists
        boolean flag = true;
 
        // If point arr[i] is valid
        for (int j = 0; j < n; j++) {
 
            // Check for the same point
            if (j == i)
                continue;
 
            // Check for a valid point
            if (arr[j][0] >= arr[i][0]
                || arr[j][1] >= arr[i][1]) {
                flag = false;
                break;
            }
        }
 
        // If current point is the
        // required point
        if (flag)
            return arr[i];
 
        // Otherwise
        return findMaxPoint(arr, i + 1, n);
    }
 
    // Function to find the required point
    static void findMaxPoints(int arr[][],
                              int n)
    {
        // Stores the point with maximum
        // X and Y-coordinates
        int ans[] = findMaxPoint(arr, 0, n);
 
        // If no required point exists
        if (ans[0] == INF) {
            System.out.println(-1);
        }
        else {
            System.out.println(
                "(" + ans[0] + " "
                + ans[1] + ")");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array of points
        int arr[][] = new int[][] {{ 1, 2 }, { 2, 1 },
                                   { 3, 4 }, { 4, 3 },
                                   { 5, 5 }};
 
        int N = arr.length;
 
        // Function Call
        findMaxPoints(arr, N);
    }
}


Python3
# Python3 program for the above approach
import  sys
 
# Initialize INF as inifnity
INF = sys.maxsize;
 
# Function to return the pohaving
# maximum X and Y coordinates
def findMaxPoint(arr, i, n):
   
    # Base Case
    if (i == n):
        return [INF, INF]
 
    # Stores if valid poexists
    flag = True;
 
    # If poarr[i] is valid
    for j in range(n):
 
        # Check for the same point
        if (j == i):
            continue;
 
        # Check for a valid point
        if (arr[j][0] >= arr[i][0] or arr[j][1] >= arr[i][1]):
            flag = False;
            break;
 
    # If current pois the
    # required point
    if (flag):
        return arr[i];
 
    # Otherwise
    return findMaxPoint(arr, i + 1, n);
 
# Function to find the required point
def findMaxPoints(arr, n):
   
    # Stores the powith maximum
    # X and Y-coordinates
    ans = findMaxPoint(arr, 0, n);
 
    # If no required poexists
    if (ans[0] == INF):
        print(-1);
    else:
        print("(" , ans[0] , " " , ans[1] , ")");
 
# Driver Code
if __name__ == '__main__':
   
    # Given array of points
    arr = [[1, 2],
           [2, 1],
           [3, 4],
           [4, 3],
           [5, 5]];
 
    N = len(arr);
 
    # Function Call
    findMaxPoints(arr, N);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Initialize INF as inifnity
static int INF = int.MaxValue;
 
// Function to return the point having
// maximum X and Y coordinates
static int[] findMaxPoint(int [,]arr, int i,
                          int n)
{
     
    // Base Case
    if (i == n)
        return new int[]{INF, INF};
 
    // Stores if valid point exists
    bool flag = true;
 
    // If point arr[i] is valid
    for(int j = 0; j < n; j++)
    {
         
        // Check for the same point
        if (j == i)
            continue;
 
        // Check for a valid point
        if (arr[j, 0] >= arr[i, 0] ||
            arr[j, 1] >= arr[i, 1])
        {
            flag = false;
            break;
        }
    }
 
    // If current point is the
    // required point
    int []ans  = new int[arr.GetLength(1)];
    if (flag)
    {
        for(int k = 0; k < ans.GetLength(0); k++)
            ans[k] = arr[i, k];
             
        return ans;
    }
 
    // Otherwise
    return findMaxPoint(arr, i + 1, n);
}
 
// Function to find the required point
static void findMaxPoints(int [,]arr,
                          int n)
{
     
    // Stores the point with maximum
    // X and Y-coordinates
    int []ans = findMaxPoint(arr, 0, n);
 
    // If no required point exists
    if (ans[0] == INF)
    {
        Console.WriteLine(-1);
    }
    else
    {
        Console.WriteLine("(" + ans[0] + " " +
                                ans[1] + ")");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array of points
    int [,]arr = new int[,]{ { 1, 2 }, { 2, 1 },
                             { 3, 4 }, { 4, 3 },
                             { 5, 5 } };
 
    int N = arr.GetLength(0);
 
    // Function Call
    findMaxPoints(arr, N);
}
}
 
// This code is contributed by Princi Singh


C++
// C++ program for the above approach
#include 
using namespace std;
#define N 5
#define P 2
 
// Function to find the point having
// max X and Y coordinates
void findMaxPoint(int arr[N][P])
{
     
    // Initialize maxX and maxY
    int maxX = INT_MIN;
    int maxY = INT_MIN;
 
    // Length of the given array
    int n = N;
 
    // Get maximum X & Y coordinates
    for(int i = 0; i < n; i++)
    {
        maxX = max(maxX, arr[i][0]);
        maxY = max(maxY, arr[i][1]);
    }
 
    // Check if the required point
    // i.e., (maxX, maxY) is present
    for(int i = 0; i < n; i++)
    {
         
        // If point with maximum X and
        // Y coordinates is present
        if (maxX == arr[i][0] &&
            maxY == arr[i][1])
        {
            cout << "(" << maxX << ", "
                        << maxY << ")";
                  
            return;
        }
    }
 
    // If no such point exists
    cout << (-1);
}
 
// Driver Code
int main()
{
     
    // Given array of points
    int arr[N][P] = { { 1, 2 }, { 2, 1 },
                      { 3, 4 }, { 4, 3 },
                      { 5, 5 } };
 
    // Print answer
    findMaxPoint(arr);
}
 
// This code is contributed by 29AjayKumar


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the point having
    // max X and Y coordinates
    static void findMaxPoint(int arr[][])
    {
        // Initialize maxX and maxY
        int maxX = Integer.MIN_VALUE;
        int maxY = Integer.MIN_VALUE;
 
        // Length of the given array
        int n = arr.length;
 
        // Get maximum X & Y coordinates
        for (int i = 0; i < n; i++) {
            maxX = Math.max(maxX, arr[i][0]);
            maxY = Math.max(maxY, arr[i][1]);
        }
 
        // Check if the required point
        // i.e., (maxX, maxY) is present
        for (int i = 0; i < n; i++) {
 
            // If point with maximum X and
            // Y coordinates is present
            if (maxX == arr[i][0]
                && maxY == arr[i][1]) {
 
                System.out.println(
                    "(" + maxX + ", "
                    + maxY + ")");
                return;
            }
        }
 
        // If no such point exists
        System.out.println(-1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array of points
        int arr[][] = new int[][] {{ 1, 2 }, { 2, 1 },
                                   { 3, 4 }, { 4, 3 },
                                   { 5, 5 }};
 
        // Print answer
        findMaxPoint(arr);
    }
}


Python3
# Python3 program for the above approach
import sys;
 
# Function to find the pohaving
# max X and Y coordinates
def findMaxPoint(arr):
     
    # Initialize maxX and maxY
    maxX = -sys.maxsize;
    maxY = -sys.maxsize;
 
    # Length of the given array
    n = len(arr);
 
    # Get maximum X & Y coordinates
    for i in range(n):
        maxX = max(maxX, arr[i][0]);
        maxY = max(maxY, arr[i][1]);
 
    # Check if the required point
    # i.e., (maxX, maxY) is present
    for i in range(n):
 
        # If powith maximum X and
        # Y coordinates is present
        if (maxX == arr[i][0] and maxY == arr[i][1]):
            print("(" , maxX , ", " , maxY , ")");
            return;
 
    # If no such poexists
    print(-1);
 
# Driver Code
if __name__ == '__main__':
   
    # Given array of points
    arr = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 5]];
 
    # Pranswer
    findMaxPoint(arr);
 
# This code is contributed by gauravrajput1


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the point having
// max X and Y coordinates
static void findMaxPoint(int [,]arr)
{
     
    // Initialize maxX and maxY
    int maxX = int.MinValue;
    int maxY = int.MinValue;
 
    // Length of the given array
    int n = arr.GetLength(0);
 
    // Get maximum X & Y coordinates
    for(int i = 0; i < n; i++)
    {
        maxX = Math.Max(maxX, arr[i, 0]);
        maxY = Math.Max(maxY, arr[i, 1]);
    }
 
    // Check if the required point
    // i.e., (maxX, maxY) is present
    for(int i = 0; i < n; i++)
    {
         
        // If point with maximum X and
        // Y coordinates is present
        if (maxX == arr[i, 0] &&
            maxY == arr[i, 1])
        {
            Console.WriteLine("(" + maxX + ", " +
                                    maxY + ")");
            return;
        }
    }
 
    // If no such point exists
    Console.WriteLine(-1);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array of points
    int [,]arr = new int[,]{ { 1, 2 }, { 2, 1 },
                             { 3, 4 }, { 4, 3 },
                             { 5, 5 } };
 
    // Print answer
    findMaxPoint(arr);
}
}
 
// This code is contributed by Amit Katiyar


输出:
(5 5)

时间复杂度: O(N 2 ),其中N是给定数组的长度。
辅助空间: O(N)

高效的方法:这个想法是找到最大的XY坐标。让它们成为maxXmaxY 。再次遍历给定的数组,检查是否存在点( maxXmaxY )。请按照以下步骤解决问题:

  1. 遍历给定的数组arr []并找到最大的XY坐标。让它们成为maxXmaxY
  2. 再次从i = 0遍历数组arr []到N-1,检查(arr [i] .X,arr [i] .Y)是否等于(maxX,maxY)
  3. 如果(maxX,maxY)存在于数组arr []中,则打印(maxX,maxY)否则打印-1

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
#define N 5
#define P 2
 
// Function to find the point having
// max X and Y coordinates
void findMaxPoint(int arr[N][P])
{
     
    // Initialize maxX and maxY
    int maxX = INT_MIN;
    int maxY = INT_MIN;
 
    // Length of the given array
    int n = N;
 
    // Get maximum X & Y coordinates
    for(int i = 0; i < n; i++)
    {
        maxX = max(maxX, arr[i][0]);
        maxY = max(maxY, arr[i][1]);
    }
 
    // Check if the required point
    // i.e., (maxX, maxY) is present
    for(int i = 0; i < n; i++)
    {
         
        // If point with maximum X and
        // Y coordinates is present
        if (maxX == arr[i][0] &&
            maxY == arr[i][1])
        {
            cout << "(" << maxX << ", "
                        << maxY << ")";
                  
            return;
        }
    }
 
    // If no such point exists
    cout << (-1);
}
 
// Driver Code
int main()
{
     
    // Given array of points
    int arr[N][P] = { { 1, 2 }, { 2, 1 },
                      { 3, 4 }, { 4, 3 },
                      { 5, 5 } };
 
    // Print answer
    findMaxPoint(arr);
}
 
// This code is contributed by 29AjayKumar

Java

// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the point having
    // max X and Y coordinates
    static void findMaxPoint(int arr[][])
    {
        // Initialize maxX and maxY
        int maxX = Integer.MIN_VALUE;
        int maxY = Integer.MIN_VALUE;
 
        // Length of the given array
        int n = arr.length;
 
        // Get maximum X & Y coordinates
        for (int i = 0; i < n; i++) {
            maxX = Math.max(maxX, arr[i][0]);
            maxY = Math.max(maxY, arr[i][1]);
        }
 
        // Check if the required point
        // i.e., (maxX, maxY) is present
        for (int i = 0; i < n; i++) {
 
            // If point with maximum X and
            // Y coordinates is present
            if (maxX == arr[i][0]
                && maxY == arr[i][1]) {
 
                System.out.println(
                    "(" + maxX + ", "
                    + maxY + ")");
                return;
            }
        }
 
        // If no such point exists
        System.out.println(-1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array of points
        int arr[][] = new int[][] {{ 1, 2 }, { 2, 1 },
                                   { 3, 4 }, { 4, 3 },
                                   { 5, 5 }};
 
        // Print answer
        findMaxPoint(arr);
    }
}

Python3

# Python3 program for the above approach
import sys;
 
# Function to find the pohaving
# max X and Y coordinates
def findMaxPoint(arr):
     
    # Initialize maxX and maxY
    maxX = -sys.maxsize;
    maxY = -sys.maxsize;
 
    # Length of the given array
    n = len(arr);
 
    # Get maximum X & Y coordinates
    for i in range(n):
        maxX = max(maxX, arr[i][0]);
        maxY = max(maxY, arr[i][1]);
 
    # Check if the required point
    # i.e., (maxX, maxY) is present
    for i in range(n):
 
        # If powith maximum X and
        # Y coordinates is present
        if (maxX == arr[i][0] and maxY == arr[i][1]):
            print("(" , maxX , ", " , maxY , ")");
            return;
 
    # If no such poexists
    print(-1);
 
# Driver Code
if __name__ == '__main__':
   
    # Given array of points
    arr = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 5]];
 
    # Pranswer
    findMaxPoint(arr);
 
# This code is contributed by gauravrajput1

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the point having
// max X and Y coordinates
static void findMaxPoint(int [,]arr)
{
     
    // Initialize maxX and maxY
    int maxX = int.MinValue;
    int maxY = int.MinValue;
 
    // Length of the given array
    int n = arr.GetLength(0);
 
    // Get maximum X & Y coordinates
    for(int i = 0; i < n; i++)
    {
        maxX = Math.Max(maxX, arr[i, 0]);
        maxY = Math.Max(maxY, arr[i, 1]);
    }
 
    // Check if the required point
    // i.e., (maxX, maxY) is present
    for(int i = 0; i < n; i++)
    {
         
        // If point with maximum X and
        // Y coordinates is present
        if (maxX == arr[i, 0] &&
            maxY == arr[i, 1])
        {
            Console.WriteLine("(" + maxX + ", " +
                                    maxY + ")");
            return;
        }
    }
 
    // If no such point exists
    Console.WriteLine(-1);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array of points
    int [,]arr = new int[,]{ { 1, 2 }, { 2, 1 },
                             { 3, 4 }, { 4, 3 },
                             { 5, 5 } };
 
    // Print answer
    findMaxPoint(arr);
}
}
 
// This code is contributed by Amit Katiyar
输出:
(5, 5)

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