Leetcode 2224: Minimum Number of Operations to Convert Time

grid47
grid47
Exploring patterns and algorithms
Mar 29, 2024 5 min read

You are given two 24-hour formatted times, current and correct. Each time is represented in the format ‘HH:MM’, where HH is the hour (00 to 23) and MM is the minutes (00 to 59). In one operation, you can increase the current time by 1, 5, 15, or 60 minutes. Your task is to determine the minimum number of operations required to convert the current time to the correct time.
Problem
Approach
Steps
Complexity
Input: You are given two strings `current` and `correct`, both in the format 'HH:MM', where the time is represented as a 24-hour clock.
Example: current = '08:30', correct = '09:20'
Constraints:
• current and correct are in the format 'HH:MM'
• current <= correct
Output: Return the minimum number of operations needed to convert the `current` time to the `correct` time.
Example: Output: 2
Constraints:
• The input strings `current` and `correct` will always be valid 24-hour time strings.
Goal: The goal is to calculate the difference in minutes between `current` and `correct`, then apply the largest operations first (60 minutes, 15 minutes, 5 minutes, and 1 minute) to minimize the number of operations.
Steps:
• Convert the current and correct times to minutes.
• Calculate the difference between the two times in minutes.
• Iterate over the available operations (60, 15, 5, and 1 minute) to minimize the number of operations.
Goal: The input strings `current` and `correct` will always be valid and follow the 24-hour time format.
Steps:
• The strings will always be valid 24-hour formatted times.
• current is less than or equal to correct.
Assumptions:
• The time difference will always be positive or zero.
Input: Input: current = '08:30', correct = '09:20'
Explanation: The difference is 50 minutes. We can first add 60 minutes (to make it 09:30), and then subtract 10 minutes to reach 09:20. This requires 2 operations.

Input: Input: current = '06:10', correct = '07:05'
Explanation: The difference is 55 minutes. First, add 60 minutes (to make it 07:10), then subtract 5 minutes to reach 07:05. This requires 2 operations.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus