Skip to the code
cf.
+25 XP

#579 Fewest Clicks to Pin Every Tooltip

medium

A chart shows tooltips that stretch horizontally; tooltip i covers the x positions from spans[i][0] to spans[i][1], both ends included. One click at position x pins every tooltip with start <= x <= end.

Write fewestClicks(spans) that returns the smallest number of clicks that pin every tooltip.

Example 1

Input: spans = [[11,15],[3,9],[2,5],[8,13]]
Output: 2

Click at 5 (pins [3, 9] and [2, 5]) and at 13 (pins [11, 15] and [8, 13]).

Example 2

Input: spans = [[1,2],[3,4],[5,6]]
Output: 3

No two tooltips share a position.

Constraints

  • 0 <= spans.length <= 100000
  • -2^31 <= start <= end <= 2^31 - 1
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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