A design system offers a fixed type scale: the font sizes in sizes, sorted ascending with no repeats. When a designer pastes text with an arbitrary size, the editor snaps it to the closest size on the scale.
Write closestSize(sizes, target) that returns the size in sizes closest to target. If two sizes are equally close, return the smaller one.
The scale can be long (think every half pixel), so use O(log n) time.
Input: sizes = [12,14,16,20,24,32], target = 19 Output: 20
20 is 1 away, closer than 16, which is 3 away.
Input: sizes = [12,14,16,20,24,32], target = 18 Output: 16
16 and 20 are both 2 away, so pick the smaller one.
1 <= sizes.length <= 100000sizes is sorted ascending with no repeats.1 <= sizes[i], target <= 10^6Run 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).
I'm here to help with #159 Closest Font Size. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.