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 _theminimum number of operations needed to convert _currenttocorrect.
Input: current ="02:30", correct ="04:35"Output: 3Explanation: We can convert current to correct in3 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.
Input: current ="11:00", correct ="11:01"Output: 1Explanation: We only have to add one minute to current, so the minimum number of operations needed is1.
#include<string>usingnamespace std;
classSolution {
public:int convertTime(string current, string correct) {
int c = stoi(current.substr(0,2)) *60+ stoi(current.substr(3,2));
int t = stoi(correct.substr(0,2)) *60+ stoi(correct.substr(3,2));
int diff = t - c, ans =0;
for (int step : {60, 15, 5, 1}) {
ans += diff / step;
diff %= step;
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import"strconv"funcconvertTime(current, correctstring) int {
c:=atoi(current[:2])*60+atoi(current[3:])
t:=atoi(correct[:2])*60+atoi(correct[3:])
diff, ans:=t-c, 0for_, step:=range []int{60, 15, 5, 1} {
ans+=diff/stepdiff%=step }
returnans}
funcatoi(sstring) int {
n, _:=strconv.Atoi(s)
returnn}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicintconvertTime(String current, String correct) {
int c = Integer.parseInt(current.substring(0,2)) * 60 + Integer.parseInt(current.substring(3,5));
int t = Integer.parseInt(correct.substring(0,2)) * 60 + Integer.parseInt(correct.substring(3,5));
int diff = t - c, ans = 0;
int[] steps = {60, 15, 5, 1};
for (int step : steps) {
ans += diff / step;
diff %= step;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funconvertTime(current: String, correct: String): Int {
val c = current.substring(0,2).toInt() * 60 + current.substring(3,5).toInt()
val t = correct.substring(0,2).toInt() * 60 + correct.substring(3,5).toInt()
var diff = t - c
var ans = 0for (step in listOf(60, 15, 5, 1)) {
ans += diff / step
diff %= step
}
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defconvertTime(self, current: str, correct: str) -> int:
c = int(current[:2]) *60+ int(current[3:])
t = int(correct[:2]) *60+ int(correct[3:])
diff, ans = t - c, 0for step in [60, 15, 5, 1]:
ans += diff // step
diff %= step
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnconvert_time(current: String, correct: String) -> i32 {
let c = current[0..2].parse::<i32>().unwrap() *60+ current[3..].parse::<i32>().unwrap();
let t = correct[0..2].parse::<i32>().unwrap() *60+ correct[3..].parse::<i32>().unwrap();
letmut diff = t - c;
letmut ans =0;
for step in [60, 15, 5, 1] {
ans += diff / step;
diff %= step;
}
ans
}
}