LeetCode 3143: Maximum Points Inside the Square Solution

Master LeetCode problem 3143 (Maximum Points Inside the Square), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3143. Maximum Points Inside the Square

Problem Explanation

Explanation:

To solve this problem, we can iterate through all pairs of points and calculate the distance between them. For each pair of points, we can consider the point that lies exactly in the middle of the two points. This point can be the center of a square if the distances to both points are the same and the square is not intersecting with any other points of the same tag.

  1. Iterate through all pairs of points.
  2. Calculate the distance between the points and find the middle point.
  3. Check if a square can be formed with the middle point as the center.
  4. Count the number of points that can be contained inside the square.
  5. Keep track of the maximum number of points in a valid square.

Time Complexity: O(n^2) where n is the number of points. Space Complexity: O(1)

:

Solution Code

class Solution {
    public int countPoints(int[][] points, String s) {
        int maxPoints = 0;
        for (int i = 0; i < points.length; i++) {
            for (int j = i + 1; j < points.length; j++) {
                int x1 = points[i][0];
                int y1 = points[i][1];
                int x2 = points[j][0];
                int y2 = points[j][1];
                
                int midX = (x1 + x2) / 2;
                int midY = (y1 + y2) / 2;
                
                int dist = Math.max(Math.abs(x1 - midX), Math.abs(y1 - midY));
                
                int pointsInside = 0;
                for (int k = 0; k < points.length; k++) {
                    if (Math.abs(points[k][0] - midX) <= dist && Math.abs(points[k][1] - midY) <= dist && s.charAt(k) != s.charAt(i)) {
                        pointsInside++;
                    }
                }
                
                maxPoints = Math.max(maxPoints, pointsInside);
            }
        }
        
        return maxPoints;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3143 (Maximum Points Inside the Square)?

This page provides optimized solutions for LeetCode problem 3143 (Maximum Points Inside the Square) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3143 (Maximum Points Inside the Square)?

The time complexity for LeetCode 3143 (Maximum Points Inside the Square) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3143 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3143 in Java, C++, or Python.

Back to LeetCode Solutions