Skip to the code
+25 XP

#94 Compress Point Coordinates

medium

A scatter-plot widget wants to draw points on a small grid that keeps their left-to-right and bottom-to-top order but drops the distances. The points are [x, y] pairs in points.

Write compressPoints(points) that replaces each x with its 0-based rank among the distinct x values and each y with its 0-based rank among the distinct y values. Return the new pairs in the same order.

Example 1

Input: points = [[100,5],[3,5],[100,900],[-7,40]]
Output: [[2,0],[1,0],[2,2],[0,1]]

The distinct x values in order are -7, 3, 100, and the distinct y values are 5, 40, 900.

Example 2

Input: points = []
Output: []

No points.

Constraints

  • 0 <= points.length <= 2 * 10^5
  • -10^9 <= x, y <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).