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.
Examples:
Input: A = 1, B = 5, N = 300
Output: 17
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 = 18Input: A = 2, B = 1, N = 1
Output: 3
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.
C++14
// C++ program to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to get the minimum time int minCostTime( int moveCost, int pushCost, int targetSeconds) { // Setting startAt to -1 as // nothing is pressed earlier // This variable shows last pressed button int startAt = -1; // Its the first timing which can be set int target_minute = targetSeconds / 60; int target_sec = targetSeconds % 60; // If time is greater than 99 min // and 60 sec then first time would come // somewhere in 100 min // which would actually be wrong. // as max min can be 99 only. if (target_minute > 99) { target_minute = target_minute - 1; target_sec = 60 + target_sec; } // Its the variables for // second timing possible int target_sec2 = 0; int target_minute2 = target_minute - 1; // Second timing is only possible // if first timing minute is // greater than zero and second // is less than 40 because // x mins and y seconds will be converted // to x-1 mins and 60 + y secs // and max seconds can be 99 if (target_sec < 40 && target_minute != 0) { target_minute2 = target_minute - 1; target_sec2 = 60 + target_sec; } // Convert 1st minute to optimal string string s = "" ; // Append min only if min > 0 // else, will be prepended automatically. if (target_minute > 0) s += to_string(target_minute); // If seconds is 1 digit number // then a 0 needs to be added before it if (target_sec < 10 && target_minute != 0) { s += '0' ; } s += to_string(target_sec); // Calculate cost for 1st TIME. int temp = startAt; int ans = 0; for ( int i = 0; i < s.length(); i++) { int x = s[i] - '0' ; if (x != temp) { ans += moveCost; ans += pushCost; temp = x; } else { ans += pushCost; temp = x; } } if (target_sec >= 40 || target_minute == 0) { return ans; } // Find cost for 2nd TIME. */ string s2 = "" ; if (target_minute2 != 0) s2 += to_string(target_minute2); if (target_sec2 < 10 && target_minute2 != 0) { s2 += '0' ; } s2 += to_string(target_sec2); int temp2 = startAt; int ans2 = 0; for ( int i = 0; i < s2.size(); i++) { int x = s2[i] - '0' ; if (x != temp2) { ans2 += moveCost; ans2 += pushCost; temp2 = x; } else { ans2 += pushCost; temp2 = x; } } // Returning the minimum of the two cost. return min(ans, ans2); } // Driver code int main() { int A = 1, B = 5, N = 300; // Function call int ans = minCostTime(A, B, N); cout << ans; return 0; } |
Java
import java.util.*; class GFG { // Function to get the minimum time public static int minCostTime( int moveCost, int pushCost, int targetSeconds) { // Setting startAt to -1 as // nothing is pressed earlier // This variable shows last pressed button int startAt = - 1 ; // Its the first timing which can be set int target_minute = targetSeconds / 60 ; int target_sec = targetSeconds % 60 ; // If time is greater than 99 min // and 60 sec then first time would come // somewhere in 100 min // which would actually be wrong. // as max min can be 99 only. if (target_minute > 99 ) { target_minute = target_minute - 1 ; target_sec = 60 + target_sec; } // Its the variables for // second timing possible int target_sec2 = 0 ; int target_minute2 = target_minute - 1 ; // Second timing is only possible // if first timing minute is // greater than zero and second // is less than 40 because // x mins and y seconds will be converted // to x-1 mins and 60 + y secs // and max seconds can be 99 if (target_sec < 40 && target_minute != 0 ) { target_minute2 = target_minute - 1 ; target_sec2 = 60 + target_sec; } // Convert 1st minute to optimal string String s = "" ; // Append min only if min > 0 // else, will be prepended automatically. if (target_minute > 0 ) s += Integer.toString(target_minute); // If seconds is 1 digit number // then a 0 needs to be added before it if (target_sec < 10 && target_minute != 0 ) { s += '0' ; } s += Integer.toString(target_sec); // Calculate cost for 1st TIME. int temp = startAt; int ans = 0 ; for ( int i = 0 ; i < s.length(); i++) { int x = s.charAt(i); if (x != temp) { ans += moveCost; ans += pushCost; temp = x; } else { ans += pushCost; temp = x; } } if (target_sec >= 40 || target_minute == 0 ) { return ans; } // Find cost for 2nd TIME. */ String s2 = "" ; if (target_minute2 != 0 ) s2 += Integer.toString(target_minute2); if (target_sec2 < 10 && target_minute2 != 0 ) { s2 += '0' ; } s2 += Integer.toString(target_sec2); int temp2 = startAt; int ans2 = 0 ; for ( int i = 0 ; i < s2.length(); i++) { int x = s2.charAt(i); if (x != temp2) { ans2 += moveCost; ans2 += pushCost; temp2 = x; } else { ans2 += pushCost; temp2 = x; } } // Returning the minimum of the two cost. return Math.min(ans, ans2); } // Driver code public static void main(String[] args) { int A = 1 , B = 5 , N = 300 ; // Function call int ans = minCostTime(A, B, N); System.out.print(ans); } } // This code is contributed by Taranpreet |
Python3
# Python program to implement the above approach # Function to get the minimum time def minCostTime(moveCost, pushCost, targetSeconds): # Setting startAt to -1 as # nothing is pressed earlier # This variable shows last pressed button startAt = - 1 # Its the first timing which can be set target_minute = targetSeconds / / 60 target_sec = targetSeconds % 60 # If time is greater than 99 min # and 60 sec then first time would come # somewhere in 100 min # which would actually be wrong. # as max min can be 99 only. if (target_minute > 99 ): target_minute = target_minute - 1 target_sec = 60 + target_sec # Its the variables for # second timing possible target_sec2 = 0 target_minute2 = target_minute - 1 # Second timing is only possible # if first timing minute is # greater than zero and second # is less than 40 because # x mins and y seconds will be converted # to x-1 mins and 60 + y secs # and max seconds can be 99 if (target_sec < 40 and target_minute ! = 0 ): target_minute2 = target_minute - 1 target_sec2 = 60 + target_sec # Convert 1st minute to optimal string s = "" # Append min only if min > 0 # else, will be prepended automatically. if (target_minute > 0 ): s = s + str (target_minute) # If seconds is 1 digit number # then a 0 needs to be added before it if (target_sec < 10 and target_minute ! = 0 ): s = s + '0' s = s + str (target_sec) # Calculate cost for 1st TIME. temp = startAt ans = 0 for i in range ( 0 , len (s)): x = ord (s[i]) if (x ! = temp): ans = ans + moveCost ans = ans + pushCost temp = x else : ans = ans + pushCost temp = x if (target_sec > = 40 or target_minute = = 0 ): return ans # Find cost for 2nd TIME. */ s2 = "" if (target_minute2 ! = 0 ): s2 = s2 + str (target_minute2) if (target_sec2 < 10 and target_minute2 ! = 0 ): s2 = s2 + '0' s2 = s2 + str (target_sec2) temp2 = startAt ans2 = 0 for i in range ( 0 , len (s2)): x = ord (s2[i]) if (x ! = temp2): ans2 = ans2 + moveCost ans2 = ans2 + pushCost temp2 = x else : ans2 = ans2 + pushCost temp2 = x # Returning the minimum of the two cost. return min (ans, ans2) # Driver code A = 1 B = 5 N = 300 # Function call ans = minCostTime(A, B, N) print (ans) # This code is contributed by Taranpreet |
C#
using System; class GFG { // Function to get the minimum time static int minCostTime( int moveCost, int pushCost, int targetSeconds) { // Setting startAt to -1 as // nothing is pressed earlier // This variable shows last pressed button int startAt = -1; // Its the first timing which can be set int target_minute = targetSeconds / 60; int target_sec = targetSeconds % 60; // If time is greater than 99 min // and 60 sec then first time would come // somewhere in 100 min // which would actually be wrong. // as max min can be 99 only. if (target_minute > 99) { target_minute = target_minute - 1; target_sec = 60 + target_sec; } // Its the variables for // second timing possible int target_sec2 = 0; int target_minute2 = target_minute - 1; // Second timing is only possible // if first timing minute is // greater than zero and second // is less than 40 because // x mins and y seconds will be converted // to x-1 mins and 60 + y secs // and max seconds can be 99 if (target_sec < 40 && target_minute != 0) { target_minute2 = target_minute - 1; target_sec2 = 60 + target_sec; } // Convert 1st minute to optimal string string s = "" ; // Append min only if min > 0 // else, will be prepended automatically. if (target_minute > 0) s += target_minute.ToString();; // If seconds is 1 digit number // then a 0 needs to be added before it if (target_sec < 10 && target_minute != 0) { s += '0' ; } s += target_sec.ToString();; // Calculate cost for 1st TIME. int temp = startAt; int ans = 0; for ( int i = 0; i < s.Length; i++) { int x = s[i]; if (x != temp) { ans += moveCost; ans += pushCost; temp = x; } else { ans += pushCost; temp = x; } } if (target_sec >= 40 || target_minute == 0) { return ans; } // Find cost for 2nd TIME. */ string s2 = "" ; if (target_minute2 != 0) s2 += target_minute2.ToString();; if (target_sec2 < 10 && target_minute2 != 0) { s2 += '0' ; } s2 += target_sec2.ToString();; int temp2 = startAt; int ans2 = 0; for ( int i = 0; i < s2.Length; i++) { int x = s2[i]; if (x != temp2) { ans2 += moveCost; ans2 += pushCost; temp2 = x; } else { ans2 += pushCost; temp2 = x; } } // Returning the minimum of the two cost. return Math.Min(ans, ans2); } // Driver code public static void Main() { int A = 1, B = 5, N = 300; // Function call int ans = minCostTime(A, B, N); Console.Write(ans); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Function to get the minimum time function minCostTime(moveCost, pushCost, targetSeconds) { // Setting startAt to -1 as // nothing is pressed earlier // This variable shows last pressed button let startAt = -1; // Its the first timing which can be set let target_minute = targetSeconds / 60; let target_sec = targetSeconds % 60; // If time is greater than 99 min // and 60 sec then first time would come // somewhere in 100 min // which would actually be wrong. // as max min can be 99 only. if (target_minute > 99) { target_minute = target_minute - 1; target_sec = 60 + target_sec; } // Its the variables for // second timing possible let target_sec2 = 0; let target_minute2 = target_minute - 1; // Second timing is only possible // if first timing minute is // greater than zero and second // is less than 40 because // x mins and y seconds will be converted // to x-1 mins and 60 + y secs // and max seconds can be 99 if (target_sec < 40 && target_minute != 0) { target_minute2 = target_minute - 1; target_sec2 = 60 + target_sec; } // Convert 1st minute to optimal string let s = "" ; // Append min only if min > 0 // else, will be prepended automatically. if (target_minute > 0) s += (target_minute).toString(); // If seconds is 1 digit number // then a 0 needs to be added before it if (target_sec < 10 && target_minute != 0) { s += '0' ; } s += (target_sec).toString(); // Calculate cost for 1st TIME. let temp = startAt; let ans = 0; for (let i = 0; i < s.length; i++) { let x = s[i].charCodeAt(0) - '0' .charCodeAt(0); if (x != temp) { ans += moveCost; ans += pushCost; temp = x; } else { ans += pushCost; temp = x; } } if (target_sec >= 40 || target_minute == 0) { return ans; } // Find cost for 2nd TIME. */ let s2 = "" ; if (target_minute2 != 0) s2 += (target_minute2).toString(); if (target_sec2 < 10 && target_minute2 != 0) { s2 += '0' ; } s2 += (target_sec2).toString(); let temp2 = startAt; let ans2 = 0; for (let i = 0; i < s2.length; i++) { let x = s2[i].charCodeAt(0) - '0' .charCodeAt(0); if (x != temp2) { ans2 += moveCost; ans2 += pushCost; temp2 = x; } else { ans2 += pushCost; temp2 = x; } } // Returning the minimum of the two cost. return Math.min(ans, ans2); } // Driver code let A = 1, B = 5, N = 300; // Function call let ans = minCostTime(A, B, N); document.write(ans) // This code is contributed by Potta Lokesh </script> |
17
Time Complexity: O(1)
Auxiliary Space: O(1).
Please Login to comment...