2224. Minimum Number of Operations to Convert Time LeetCode Solution

In this guide, you will get 2224. Minimum Number of Operations to Convert Time LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Operations to Convert Time problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Number of Operations to Convert Time solution in C++
  4. Minimum Number of Operations to Convert Time solution in Java
  5. Minimum Number of Operations to Convert Time solution in Python
  6. Additional Resources
2224. Minimum Number of Operations to Convert Time LeetCode Solution image

Problem Statement of Minimum Number of Operations to Convert Time

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2224. Minimum Number of Operations to Convert Time LeetCode Solution in C++

class Solution {
 public:
  int convertTime(string current, string correct) {
    const vector<int> ops{60, 15, 5, 1};
    int diff = getMinutes(correct) - getMinutes(current);
    int ans = 0;

    for (const int op : ops) {
      ans += diff / op;
      diff %= op;
    }

    return ans;
  }

 private:
  int getMinutes(const string& s) {
    return stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3));
  }
};
/* code provided by PROGIEZ */

2224. Minimum Number of Operations to Convert Time LeetCode Solution in Java

class Solution {
  public int convertTime(String current, String correct) {
    final int[] ops = {60, 15, 5, 1};
    int diff = getMinutes(correct) - getMinutes(current);
    int ans = 0;

    for (final int op : ops) {
      ans += diff / op;
      diff %= op;
    }

    return ans;
  }

  private int getMinutes(final String s) {
    return Integer.parseInt(s.substring(0, 2)) * 60 + Integer.parseInt(s.substring(3));
  }
}
// code provided by PROGIEZ

2224. Minimum Number of Operations to Convert Time LeetCode Solution in Python

class Solution:
  def convertTime(self, current: str, correct: str) -> int:
    ops = [60, 15, 5, 1]

    def getMinutes(s: str) -> int:
      return int(s[:2]) * 60 + int(s[3:])

    diff = getMinutes(correct) - getMinutes(current)
    ans = 0

    for op in ops:
      ans += diff // op
      diff %= op

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.