# 2224. Minimum Number of Operations to Convert Time

• Time: $O(n)$
• Space: $O(1)$
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public: int convertTime(string current, string correct) { const vector 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)); } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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)); } } 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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