Skip to main content

Minimum Number of Operations to Convert Time - Solution & Explanation

EasyStringGreedy12 min readAsked at: Google
Practice this problem

Problem Statement

You are given two strings current and correct representing two 24-hour times.

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.

In one operation you can increase the time current by 1, 5, 15, or 60 minutes. You can perform this operation any number of times.

Return the minimum number of operations needed to convert current to correct.

 

Example 1:

Input: current = "02:30", correct = "04:35"
Output: 3
Explanation:
We can convert current to correct in 3 operations as follows:
- Add 60 minutes to current. current becomes "03:30".
- Add 60 minutes to current. current becomes "04:30".
- Add 5 minutes to current. current becomes "04:35".
It can be proven that it is not possible to convert current to correct in fewer than 3 operations.

Example 2:

Input: current = "11:00", correct = "11:01"
Output: 1
Explanation: We only have to add one minute to current, so the minimum number of operations needed is 1.

 

Constraints:

  • current and correct are in the format "HH:MM"
  • current <= correct

Approach Overview

Problem Overview: You are given two time strings current and correct in HH:MM format. In one operation you can add 1, 5, 15, or 60 minutes to the current time. The goal is to reach the correct time using the minimum number of operations.

Approach 1: Greedy with Minutes Conversion (Time: O(1), Space: O(1))

Convert both times into total minutes from midnight. Compute the difference diff = correctMinutes - currentMinutes. The allowed operations are fixed increments: 60, 15, 5, and 1. Apply a greedy strategy by repeatedly taking the largest possible increment that does not exceed the remaining difference. For example, use as many 60-minute operations as possible, then 15, then 5, and finally 1. Because these increments form a canonical system, the greedy choice always produces the minimum number of operations. The computation involves a few integer divisions and remainders, so runtime is constant.

The key insight: minimizing operations is equivalent to minimizing the number of increments used to build the minute difference. With fixed step sizes, greedy selection works the same way as making change with standard coin denominations. You simply reduce the remaining minutes with the largest step first.

Relevant concepts appear frequently in greedy algorithms and basic string parsing problems.

Approach 2: Dynamic Programming (Time: O(D * 4), Space: O(D))

Dynamic programming treats the problem like a small coin change variant. Let D be the minute difference. Build a DP array where dp[i] represents the minimum operations needed to reach exactly i minutes. For each minute value, check transitions using the allowed operations {1, 5, 15, 60}. The recurrence is dp[i] = 1 + min(dp[i - step]) for valid steps.

This guarantees an optimal answer for every intermediate value and works even if the operation set changes. However, it requires iterating through all minute values up to D, so it is slower and uses extra memory. For this problem the difference is at most 1439 minutes, so it still runs fast in practice. This approach connects to classic dynamic programming patterns used in coin change problems.

Recommended for interviews: The greedy minutes-conversion approach is what interviewers expect. It shows you recognized the fixed increments and reduced the problem to a simple arithmetic strategy. The DP method demonstrates problem generalization, but greedy is the clean, optimal solution with constant time and space.

Approach 1: Greedy Approach with Minutes Conversion

The basic idea is to convert both 'current' and 'correct' times into the total number of minutes from the start of the day ('00:00'). Then, calculate the difference in minutes. Utilize the largest increments first (60, 15, 5, and 1), minimizing the total number of operations required to reach the target time.

In this implementation, we convert the 'current' and 'correct' times into total minutes. The remaining difference is minimized using the largest minute operation possible, proceeding in decreasing order of minute increments.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time complexity: O(1), since the calculations are based on constant operations.
Space complexity: O(1), since no additional space is needed apart from a few variables.

Try this approach in the editor →

Approach 2: Dynamic Programming Approach

Although dynamic programming is not required for such a small, constant problem, a conceptualization might involve formulating transitions of states as the minimal number of operations from 'current' to 'correct'. Each state would represent a specific minute alteration with a transition cost of 1, showcasing the application of dynamic programming principles for problem solving.

This pseudo-code highlights the limitations of using dynamic programming due to the simplicity and constraints of the problem, suggesting greedy solutions as more efficient for implementation.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time complexity: Hypothetically O(N) where N is the minutes between current and correct.
Space complexity: O(N), but this is impractical for the given problem size.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Approach with Minutes Conversion

Time complexity: O(1), since the calculations are based on constant operations.
Space complexity: O(1), since no additional space is needed apart from a few variables.

Dynamic Programming Approach

Time complexity: Hypothetically O(N) where N is the minutes between current and correct.
Space complexity: O(N), but this is impractical for the given problem size.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy with Minutes ConversionO(1)O(1)Best choice when operation increments are fixed (60,15,5,1). Minimal logic and constant runtime.
Dynamic Programming (Coin Change Style)O(D * 4)O(D)Useful when increment values are arbitrary or greedy optimality is not guaranteed.

Video Solution

2224. Minimum Number of Operations to Convert Time || Leetcode Weekly Contest 287 || Leetcode 2224 • Bro Coders • 1,146 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Minimum Number of Operations to Convert Time easy or hard?
LeetCode classifies this problem as Easy. The main challenge is recognizing that converting times to minutes simplifies the logic. Once the difference is known, a straightforward greedy strategy with the four allowed increments produces the minimum number of operations.
Minimum Number of Operations to Convert Time Python/Java solution
In Python or Java, parse the HH:MM string using split, convert hours and minutes into total minutes, compute the difference, and apply greedy division with increments [60, 15, 5, 1]. Each division gives the number of operations for that step, and the remainder is used for the next increment.
How to solve Minimum Number of Operations to Convert Time in O(1)?
Convert both time strings into total minutes from midnight. Compute the difference between correct and current time. Then greedily apply increments of 60, 15, 5, and 1 minute using division and modulo operations to count how many of each operation are needed. Since the number of increments is fixed, the algorithm runs in constant time.
What is the best approach for Minimum Number of Operations to Convert Time?
The greedy minutes-conversion approach is the best solution. Convert both HH:MM times into total minutes, compute the difference, then use the largest allowed increments (60, 15, 5, 1) greedily. This works because the increments form a canonical set where greedy choices always minimize operations. The algorithm runs in O(1) time and O(1) space.
Is Minimum Number of Operations to Convert Time asked at Google/Amazon/Meta?
This problem represents a classic greedy reasoning pattern that appears in interviews at companies like Amazon and Google. Variants often involve coin change, minimum steps, or greedy scheduling. Interviewers use it to test whether candidates recognize when a greedy strategy guarantees optimal results.
What data structure is used in Minimum Number of Operations to Convert Time?
No complex data structure is required for the optimal solution. The algorithm relies on simple integer arithmetic after parsing the time strings. In the dynamic programming version, an array or list is used to store the minimum operations for each minute value up to the target difference.
What is the time complexity of Minimum Number of Operations to Convert Time?
The optimal greedy solution runs in O(1) time because it performs a constant number of arithmetic operations after converting times into minutes. Space complexity is also O(1). A dynamic programming alternative runs in O(D) time and O(D) space, where D is the minute difference between the two times.

Ready to solve this problem?

Practice Minimum Number of Operations to Convert Time with our built-in code editor and test cases.

Practice on FleetCode