[LeetCode] Maximum Points Inside the Square

3143. Maximum Points Inside the Square

You are given a 2D array points and a string s where, points[i] represents the coordinates of point i, and s[i] represents the tag of point i.

A valid square is a square centered at the origin (0, 0), has edges parallel to the axes, and does not contain two points with the same tag.

Return the maximum number of points contained in a valid square.

Note:

  • A point is considered to be inside the square if it lies on or within the square’s boundaries.
  • The side length of the square can be zero.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
int maxPointsInsideSquare(vector<vector<int>>& points, string s) {
vector<int> freq(26);
vector<pair<int,char>> A;
for(int i = 0; i < s.length(); i++) {
int x = points[i][0], y = points[i][1];
int z = max(abs(x), abs(y));
A.push_back({z,s[i]});
}
sort(rbegin(A), rend(A));
int res = 0;
int cnt = 0;
bool ok = true;
while(A.size() and ok) {
int x = A.back().first;
while(A.size() and A.back().first == x) {
auto [_,ch] = A.back(); A.pop_back();
if(++freq[ch-'a'] >= 2) ok = false;
cnt++;
}
if(ok) res = cnt;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/12/PS/LeetCode/maximum-points-inside-the-square/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.