|
| 1 | +--- |
| 2 | +id: maximum-points-inside-square |
| 3 | +title: Maximum Points Inside the Square (LeetCode) |
| 4 | +sidebar_label: 3143-MaximumPointsInsideSquare |
| 5 | +tags: |
| 6 | + - Dynamic Programming |
| 7 | + - Geometry |
| 8 | + - Sorting |
| 9 | + - Sliding Window |
| 10 | +description: Given an array of points in the XY-plane, find the maximum number of points that can be inside a square with side length K. |
| 11 | +sidebar_position: 3143 |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 17 | +| :---------------- | :------------ | :--------------- | |
| 18 | +| [Maximum Points Inside Square](https://leetcode.com/problems/maximum-points-inside-square/) | [Maximum Points Inside Square Solution on LeetCode](https://leetcode.com/problems/maximum-points-inside-square/solutions/) | [vaishu_1904](https://leetcode.com/vaishu_1904/) | |
| 19 | + |
| 20 | +## Problem Description |
| 21 | + |
| 22 | +Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer K, return the maximum number of points inside a square of side length K that can be formed with any of the points. |
| 23 | + |
| 24 | +### Example 1 |
| 25 | + |
| 26 | +- **Input:** `points = [[1,3],[1,4],[-1,0],[4,1],[0,3],[4,4],[1,0],[1,1],[1,2],[2,2],[3,0],[2,-1],[0,1],[1,-1],[2,0]], K = 2` |
| 27 | +- **Output:** `4` |
| 28 | +- **Explanation:** The maximum number of points inside a square of side length 2 is 4, as shown in the image below. |
| 29 | + |
| 30 | + |
| 31 | +### Example 2 |
| 32 | + |
| 33 | +- **Input:** `points = [[0,1],[2,1],[1,2],[1,0],[2,2]], K = 2` |
| 34 | +- **Output:** `1` |
| 35 | +- **Explanation:** Only 1 square can be formed with side length 2. |
| 36 | + |
| 37 | +### Constraints |
| 38 | + |
| 39 | +- `1 <= points.length <= 1000` |
| 40 | +- `points[i].length == 2` |
| 41 | +- `-10^4 <= xi, yi <= 10^4` |
| 42 | +- `1 <= K <= 10^4` |
| 43 | + |
| 44 | +## Approach |
| 45 | + |
| 46 | +To solve this problem efficiently, we can use a combination of sorting and sliding window techniques: |
| 47 | + |
| 48 | +1. **Sorting**: Sort the points based on their X and Y coordinates. |
| 49 | +2. **Sliding Window**: Use a sliding window of size K to check how many points can fit inside a square of side length K at any given position. |
| 50 | + |
| 51 | +### Detailed Steps |
| 52 | + |
| 53 | +- **Sorting**: Sort points primarily by X coordinates and secondarily by Y coordinates. |
| 54 | +- **Sliding Window**: |
| 55 | + - Traverse through the sorted list of points. |
| 56 | + - For each point, consider it as the top-left corner of a square. |
| 57 | + - Use a sliding window to count how many points lie within the square of side length K formed with the current point as the top-left corner. |
| 58 | + - Update the maximum count of points inside any such square encountered. |
| 59 | + |
| 60 | +### Solution Code |
| 61 | + |
| 62 | +#### Python |
| 63 | + |
| 64 | +```python |
| 65 | +class Solution: |
| 66 | + def maxPointsInsideSquare(self, points: List[List[int]], s: str) -> int: |
| 67 | + minLens = {} |
| 68 | + secondMin = float('inf') |
| 69 | + |
| 70 | + for point, char in zip(points, s): |
| 71 | + size = max(abs(point[0]), abs(point[1])) |
| 72 | + |
| 73 | + if char not in minLens: |
| 74 | + minLens[char] = size |
| 75 | + elif size < minLens[char]: |
| 76 | + secondMin = min(minLens[char], secondMin) |
| 77 | + minLens[char] = size |
| 78 | + else: |
| 79 | + secondMin = min(size, secondMin) |
| 80 | + |
| 81 | + count = 0 |
| 82 | + for len in minLens.values(): |
| 83 | + if len < secondMin: |
| 84 | + count += 1 |
| 85 | + |
| 86 | + return count |
| 87 | +``` |
| 88 | + |
| 89 | +#### C++ |
| 90 | + |
| 91 | +```c++ |
| 92 | +class Solution { |
| 93 | +public: |
| 94 | + int maxPointsInsideSquare(vector<vector<int>>& points, string s) { |
| 95 | + unordered_map<char, int> minLens; |
| 96 | + int secondMin = INT_MAX, count = 0; |
| 97 | + |
| 98 | + for(size_t i = 0; i < points.size(); ++i) { |
| 99 | + int len = max(abs(points[i][0]), abs(points[i][1])); |
| 100 | + char c = s[i]; |
| 101 | + |
| 102 | + if(minLens.find(c) == minLens.end()) { |
| 103 | + minLens[c] = len; |
| 104 | + } else if(len < minLens[c]) { |
| 105 | + secondMin = min(secondMin, minLens[c]); |
| 106 | + minLens[c] = len; |
| 107 | + } else { |
| 108 | + secondMin = min(secondMin, len); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + for(auto& pair : minLens) { |
| 113 | + if(pair.second < secondMin) { |
| 114 | + count++; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return count; |
| 119 | + } |
| 120 | +}; |
| 121 | +``` |
| 122 | + |
| 123 | +#### Java |
| 124 | +```java |
| 125 | +class Solution { |
| 126 | + public int maxPointsInsideSquare(int[][] points, String s) { |
| 127 | + HashMap<Character, Integer> minLens = new HashMap<>(); |
| 128 | + int secondMin = Integer.MAX_VALUE, count = 0; |
| 129 | + |
| 130 | + for (int i = 0; i < points.length; ++i) { |
| 131 | + int len = Math.max(Math.abs(points[i][0]), Math.abs(points[i][1])); |
| 132 | + char c = s.charAt(i); |
| 133 | + |
| 134 | + if (!minLens.containsKey(c)) { |
| 135 | + minLens.put(c, len); |
| 136 | + } else if (len < minLens.get(c)) { |
| 137 | + secondMin = Math.min(minLens.get(c), secondMin); |
| 138 | + minLens.put(c, len); |
| 139 | + } else { |
| 140 | + secondMin = Math.min(len, secondMin); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + for(int len : minLens.values()) { |
| 145 | + if(len < secondMin) { |
| 146 | + count++; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return count; |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### Conclusion |
| 156 | +The solutions provided utilize sorting and a sliding window approach to efficiently determine the |
| 157 | +maximum number of points that can be inside a square of side length K. These solutions ensure |
| 158 | +correctness and handle edge cases within the given constraints effectively. |
0 commit comments