Skip to content

Commit 57d6a82

Browse files
solves #2224: Minimum Number of Operations to Convert Time in java
1 parent 108da7c commit 57d6a82

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@
723723
| 2210 | [Count Hills and Valleys in an Array](https://leetcode.com/problems/count-hills-and-valleys-in-an-array) | [![Java](assets/java.png)](src/CountHillsAndValleysInAnArray.java) | |
724724
| 2215 | [Find the Difference of Two Arrays](https://leetcode.com/problems/find-the-difference-of-two-arrays) | [![Java](assets/java.png)](src/FindTheDifferenceOfTwoArrays.java) | |
725725
| 2220 | [Minimum Bit Flips to Convert Number](https://leetcode.com/problems/minimum-bit-flips-to-convert-number) | [![Java](assets/java.png)](src/MinimumBitFlipsToConvertNumber.java) | |
726-
| 2224 | [Minimum Number of Operations to Convert Time](https://leetcode.com/problems/minimum-number-of-operations-to-convert-time) | | |
726+
| 2224 | [Minimum Number of Operations to Convert Time](https://leetcode.com/problems/minimum-number-of-operations-to-convert-time) | [![Java](assets/java.png)](src/MinimumNumberOfOperationsToConvertTime.java) | |
727727
| 2229 | 🔒 [Check if an array is consecutive](https://leetcode.com/problems/check-if-an-array-is-consecutive) | | |
728728
| 2231 | [Largest Number After Digit Swaps by Parity](https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity) | | |
729729
| 2235 | [Add Two Integers](https://leetcode.com/problems/add-two-integers) | | |

Diff for: src/MinimumNumberOfOperationsToConvertTime.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/minimum-number-of-operations-to-convert-time
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class MinimumNumberOfOperationsToConvertTime {
6+
private final static int[] MINUTE_INCREMENTS = {60, 15, 5, 1};
7+
8+
public int convertTime(String current, String correct) {
9+
final int hour1 = Integer.parseInt(current.substring(0, 2)), hour2 = Integer.parseInt(correct.substring(0, 2));
10+
final int minutes1 = Integer.parseInt(current.substring(3)), minutes2 = Integer.parseInt(correct.substring(3));
11+
final int totalMinutes1 = hour1 * 60 + minutes1, totalMinutes2 = hour2 * 60 + minutes2;
12+
13+
int operations = 0;
14+
int difference = totalMinutes2 - totalMinutes1;
15+
16+
for (int i = 0 ; i < MINUTE_INCREMENTS.length && difference > 0; i++) {
17+
operations += difference / MINUTE_INCREMENTS[i];
18+
difference -= (difference / MINUTE_INCREMENTS[i]) * MINUTE_INCREMENTS[i];
19+
}
20+
21+
return operations;
22+
}
23+
}

0 commit comments

Comments
 (0)