A TV has n channels numbered from 1 to n. You are on channel current and want to reach channel target. Return the minimum number of button presses on the remote control that gets you there.
The remote has these buttons, and every press costs 1:
-
Up goes to the next higher channel. It wraps from channel
n
to channel
1
.
-
Down goes to the next lower channel. It wraps from channel
1
to channel
n
.
-
Back goes to the previously visited channel. It is unavailable until you have changed channel at least once.
-
The number keys
0
to
9
type a channel number. Each digit is one press, so typing channel
123
costs 3 presses and jumps straight to channel 123.
Function Signature
def min_presses(n: int, current: int, target: int) -> int:
Rules
-
You may type any channel number from
1
to
n
, not only
target
, and keep pressing Up, Down or Back afterwards.
-
Typing channel
x
costs as many presses as
x
has digits. Numbers are typed without leading zeros, and only numbers from
1
to
n
can be typed.
-
If
current == target
, the answer is 0.
Constraints
-
1 <= n <= 10000
-
1 <= current <= n
-
1 <= target <= n
Examples
Input: n = 100, current = 15, target = 20
Output: 2
Pressing Up five times costs 5 presses, while typing 2 and 0 costs 2. No single press reaches channel 20 from channel 15, so 2 is the minimum.
Input: n = 500, current = 1, target = 500
Output: 1
Down wraps from channel 1 to channel 500 in one press, which beats typing 500 (3 presses).
Input: n = 1000, current = 500, target = 1000
Output: 2
Typing 1 jumps to channel 1, and Down then wraps to channel 1000, for 2 presses in total. Typing 1000 costs 4, walking costs 500, and no single press reaches channel 1000.