Open In App

Minimum time slots to convert time T1 to T2

Last Updated : 06 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function for calculating the minimum
// number of operation required to
// convert time1 to time2
int convertTime(string T1, string T2)
{
    // Converting strings into integer
    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));
 
    // Converting time into minutes
    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;
}
 
// Driver code
int main()
{
    // Input two strings
    string T1 = "02:30";
    string T2 = "04:35";
 
    // Function call
    int result = convertTime(T1, T2);
    cout << " " << result << endl;
    return 0;
}


Java




// Java code to implement the approach
import java.util.*;
public class GFG {
 
  // Function for calculating the minimum
  // number of operation required to
  // convert time1 to time2
  static int convertTime(String T1, String T2)
  {
     
    // Converting strings into integer
    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));
 
    // Converting time into minutes
    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;
  }
 
  // Driver code
  public static void main(String args[])
  {
    // Input two strings
    String T1 = "02:30";
    String T2 = "04:35";
 
    // Function call
    int result = convertTime(T1, T2);
    System.out.println(" " + result);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3




# Python3 code to implement the above approach
 
# Function for calculating the minimum
# number of operation required to convert
# time1 to time2
def convertTime(T1, T2):
   
    # Converting strings into integer
    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])
     
    # convertin time into minutes
    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
 
# Driver Code
 
# Input two strings
T1 = "02:30"
T2 = "04:35"
 
# Function Call
result = convertTime(T1, T2)
print(result)
         
# This code is contributed by phasing17      


C#




// C# code to implement the approach
using System;
 
public class GFG
{
     
  // Function for calculating the minimum
  // number of operation required to
  // convert time1 to time2
  static int convertTime(String T1, String T2)
  {
     
    // Converting strings into integer
    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));
 
    // Converting time into minutes
    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;
  }
   
    //Driver Code
    public static void Main(string[] args)
    {
        // Input two strings
        string T1 = "02:30";
        string T2 = "04:35";
     
        // Function call
        int result = convertTime(T1, T2);
        Console.WriteLine(" " + result);
    }
}
 
//this code is contributed by phasing17


Javascript




<script>
    // JavaScript code to implement the approach
 
    // Function for calculating the minimum
    // number of operation required to
    // convert time1 to time2
    const convertTime = (T1, T2) => {
     
        // Converting strings into integer
        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));
 
        // Converting time into minutes
        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;
    }
 
    // Driver code
 
    // Input two strings
    let T1 = "02:30";
    let T2 = "04:35";
 
    // Function call
    let result = convertTime(T1, T2);
    document.write(` ${result}`);
 
// This code is contributed by rakeshsahni
 
</script>


Output

 3

Time Complexity: O(M) where M is the time difference
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads