Given that a microwave takes key stroke inputs and a target cooking time, find the optimal input considering the following costs for input:
999
will be interpreted as 9
minutes 99
seconds.81
as 81
seconds.1221
as 12
minutes 21
seconds.
The cost of each key stroke is 1
, and the cost of moving the finger to a different key is 2
. For example, input 999
has a cost of 3
, input 1122
has a cost of 6
, and input 1234
has a cost of 10
.
The input has to be within 10%
of the target time. If the cost is the same, select the input that's closest to the target time.
For example, for a target time of 10
minutes, 888
is the optimal input (not 999
).
Function Description
Complete the function findOptimalInput
in the editor.
findOptimalInput
has the following parameter:
targetTime
: the target cooking time in seconds
Returns
int
: the optimal input value
Example 1:
Input: targetTime = 600
Output: 888
Explanation:For a target time of
10
minutes (600
seconds), the optimal input is888
, which is interpreted as8
minutes88
seconds or528
seconds. This is within10%
of the target time and has a lower cost than999
(which would be9
minutes99
seconds or599
seconds).
:)

input:
output: