Ctrl K

A Square of Unique Labels

Medium
The plane holds the points points, and point i carries a label, the letter labels[i]. Consider squares centred at the origin with sides parallel to the axes; a side of zero length is allowed. A point is inside a square if it lies within it or on its border. A square is acceptable when no two points inside it share a label. Return the largest number of points an acceptable square can contain.
Example 1
points=[[1, 0], [0, -2], [3, 3], [-2, 1]], labels='abca' → 1

A square with half-side 2 covers [1, 0], [0, -2] and [-2, 1], but the first and the last share the label a. A square with half-side 1 covers only [1, 0]. The answer is 1.

Example 2
points=[[0, 0], [2, -1], [-1, 2], [5, 0]], labels='xyzx' → 3

A square with half-side 2 covers three points labelled x, y and z. Reaching [5, 0] would bring in a second x.

Example 3
points=[[4, 4], [-4, 1]], labels='qq' → 0
Constraints
  • 1 ≤ len(points) ≤ 10⁵
  • len(labels) = len(points)
  • points[i] = [x, y], -10⁹ ≤ x, y ≤ 10⁹
  • All points are distinct
  • labels consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords Hidden in a Text MediumWeight by the Earliest Letter MediumAnagram Buckets MediumA Subsequence of One Word Only MediumPairs With a Fixed Gap Medium
loading Python…
Input
points=[[1, 0], [0, -2], [3, 3], [-2, 1]], labels='abca'
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 79 tests on the server, hidden ones included.