Minimum time slots to convert time T1 to T2
Last Updated :
06 Jun, 2022
Given two strings T1 and T2 representing two time instances in 24-hour formats, the task is to convert T1 to T2 using a minimum number of time slots among 1, 5, 15, or 60 minutes.
24-hour times are formatted as “HH: MM”, where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.
Examples:
Input: T1 = “02:30”, T2 = “04:35”
Output: 3
Explanation: The time difference between them is 2 hours and 5 minutes.
Increase T1 by 60 minutes 2 times and 1 time by 5 minutes.
Input: T1 = “11:00”, T2 = “11:01”
Output: 1
Explanation: Increased the time by 1 minutes for once.
Approach: This problem can be solved using the Greedy approach based on the following idea.
To minimize the required slots, use the ones with the highest time first and then go for the smaller ones, i.e. in the order 60, 15, 5, 1. with 60 used first and 1 used and last
Follow the below steps to implement the above idea:
- Convert strings T1 and T2 to their respective time and store them in minutes (say time1_to_min and time2_to_min).
- Initialise a variable to store the number of time slots (say operations).
- Run a until time1_to_min != time2_to_min
- Find the difference between the times (say diff).
- If diff ≥ 60 then decrement 60 from time2_to_min and increment operations by 1.
- If diff ≥ 15 and diff < 60 then decrement 15 from time2_to_min and increment operations by 1.
- If diff ≥ 5 and diff < 15 then decrement 5 from time2_to_min and increment operations by 1.
- Else decrement 1 from time2_to_min and increment operations by 1.
- Return operations as that is the minimum number of time slots required.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int convertTime(string T1, string T2)
{
int hour_time1 = stoi(T1.substr(0, 2));
int min_time1 = stoi(T1.substr(3, 4));
int hour_time2 = stoi(T2.substr(0, 2));
int min_time2 = stoi(T2.substr(3, 4));
int time1_to_min = (hour_time1 * 60) + min_time1;
int time2_to_min = (hour_time2 * 60) + min_time2;
int operations = 0;
while (time1_to_min != time2_to_min) {
int diff = time2_to_min - time1_to_min;
if (diff >= 60) {
time2_to_min -= 60;
operations++;
}
else if (diff >= 15 && diff < 60) {
time2_to_min -= 15;
operations++;
}
else if (diff >= 5 && diff < 15) {
time2_to_min -= 5;
operations++;
}
else {
time2_to_min -= 1;
operations++;
}
}
return operations;
}
int main()
{
string T1 = "02:30" ;
string T2 = "04:35" ;
int result = convertTime(T1, T2);
cout << " " << result << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int convertTime(String T1, String T2)
{
int hour_time1
= Integer.parseInt(T1.substring( 0 , 2 ));
int min_time1
= Integer.parseInt(T1.substring( 3 , 5 ));
int hour_time2
= Integer.parseInt(T2.substring( 0 , 2 ));
int min_time2
= Integer.parseInt(T2.substring( 3 , 5 ));
int time1_to_min = (hour_time1 * 60 ) + min_time1;
int time2_to_min = (hour_time2 * 60 ) + min_time2;
int operations = 0 ;
while (time1_to_min != time2_to_min) {
int diff = time2_to_min - time1_to_min;
if (diff >= 60 ) {
time2_to_min -= 60 ;
operations++;
}
else if (diff >= 15 && diff < 60 ) {
time2_to_min -= 15 ;
operations++;
}
else if (diff >= 5 && diff < 15 ) {
time2_to_min -= 5 ;
operations++;
}
else {
time2_to_min -= 1 ;
operations++;
}
}
return operations;
}
public static void main(String args[])
{
String T1 = "02:30" ;
String T2 = "04:35" ;
int result = convertTime(T1, T2);
System.out.println( " " + result);
}
}
|
Python3
def convertTime(T1, T2):
hour_time1 = int (T1[ 0 : 2 ])
min_time1 = int (T1[ 3 : 3 + 4 ])
hour_time2 = int (T2[ 0 : 2 ])
min_time2 = int (T2[ 3 : 3 + 4 ])
time1_to_min = hour_time1 * 60 + min_time1
time2_to_min = hour_time2 * 60 + min_time2
operations = 0
while time1_to_min ! = time2_to_min:
diff = time2_to_min - time1_to_min
if diff > = 60 :
time2_to_min - = 60
operations + = 1
elif diff > = 15 and diff < 60 :
time2_to_min - = 15
operations + = 1
elif diff > = 5 and diff < 15 :
time2_to_min - = 5
operations + = 1
else :
time2_to_min - = 1
operations + = 1
return operations
T1 = "02:30"
T2 = "04:35"
result = convertTime(T1, T2)
print (result)
|
C#
using System;
public class GFG
{
static int convertTime(String T1, String T2)
{
int hour_time1
= Int32.Parse(T1.Substring(0, 2));
int min_time1
= Int32.Parse(T1.Substring(3, 2));
int hour_time2
= Int32.Parse(T2.Substring(0, 2));
int min_time2
= Int32.Parse(T2.Substring(3, 2));
int time1_to_min = (hour_time1 * 60) + min_time1;
int time2_to_min = (hour_time2 * 60) + min_time2;
int operations = 0;
while (time1_to_min != time2_to_min) {
int diff = time2_to_min - time1_to_min;
if (diff >= 60) {
time2_to_min -= 60;
operations++;
}
else if (diff >= 15 && diff < 60) {
time2_to_min -= 15;
operations++;
}
else if (diff >= 5 && diff < 15) {
time2_to_min -= 5;
operations++;
}
else {
time2_to_min -= 1;
operations++;
}
}
return operations;
}
public static void Main( string [] args)
{
string T1 = "02:30" ;
string T2 = "04:35" ;
int result = convertTime(T1, T2);
Console.WriteLine( " " + result);
}
}
|
Javascript
<script>
const convertTime = (T1, T2) => {
let hour_time1 = parseInt(T1.substring(0, 2));
let min_time1 = parseInt(T1.substring(3, 3 + 4));
let hour_time2 = parseInt(T2.substring(0, 2));
let min_time2 = parseInt(T2.substring(3, 3 + 4));
let time1_to_min = (hour_time1 * 60) + min_time1;
let time2_to_min = (hour_time2 * 60) + min_time2;
let operations = 0;
while (time1_to_min != time2_to_min) {
let diff = time2_to_min - time1_to_min;
if (diff >= 60) {
time2_to_min -= 60;
operations++;
}
else if (diff >= 15 && diff < 60) {
time2_to_min -= 15;
operations++;
}
else if (diff >= 5 && diff < 15) {
time2_to_min -= 5;
operations++;
}
else {
time2_to_min -= 1;
operations++;
}
}
return operations;
}
let T1 = "02:30" ;
let T2 = "04:35" ;
let result = convertTime(T1, T2);
document.write(` ${result}`);
</script>
|
Time Complexity: O(M) where M is the time difference
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...