Minimum Cost To set Digital Clock Timer with given movement and push cost
Given integers A, B and N. The task is to minimize the cost to set N seconds in a digital clock where time is represented in the following format:
- at most 99 minutes and 99 seconds
- at least 1 second
- The first two digits represent minutes and the last two minutes represent seconds.
- It prepends 0 if less than 4 digits are pressed to set time
A is the cost to get a new digit that is already not pressed to set time and B is the cost for pressing any digit to set time.
Input: A = 1, B = 5, N = 300
Explanation: The following possible clock settings can be: 05:00, 5:00, 04:60, 4:60.
since 4 minutes, 60 seconds is equivalent to 5 minutes.
If the clock is set as 5:00 it will require 1 + 5 to set 5, then 1 + 5 to set a zero, then 5 to set the last zero,
since the same button is pressed again, no requirement of adding A.
So minimum cost = 1 + 5 + 1+ 5 + 5 = 17
The other option of 4:60 gives cost = 1 + 5 + 1 + 5 + 1 + 5 = 18
Input: A = 2, B = 1, N = 1
Approach: This problem is implementation based. Find out the possible two representations (may have only one possible representation) and the minimum cost among those two. Follow the steps mentioned below:
- The first observation should be that there is no need of pressing 0s which would be prepended by the clock automatically.
- So, find the two clock timings: (x/60, x%60) and (x/60 -1, 60 + x%60) no other combination is possible.
- Try to find the best answer between these two timings only.
Below is the implementation of the above approach.
Time Complexity: O(1)
Auxiliary Space: O(1).
Please Login to comment...