Skip to content

Commit 75df5ad

Browse files
authored
Update 1904-the-number-of-full-rounds-you-have-played.js
1 parent 2b0631a commit 75df5ad

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

1904-the-number-of-full-rounds-you-have-played.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,28 @@
44
* @return {number}
55
*/
66
const numberOfRounds = function(startTime, finishTime) {
7-
let start = 60 * parseInt(startTime.slice(0, 2)) + parseInt(startTime.slice(3))
8-
let finish = 60 * parseInt(finishTime.slice(0, 2)) + parseInt(finishTime.slice(3));
9-
if (start > finish) finish += 60 * 24; // If `finishTime` is earlier than `startTime`, add 24 hours to `finishTime`.
10-
return Math.floor(finish / 15) - Math.ceil(start / 15); // floor(finish / 15) - ceil(start / 15)
7+
const { ceil, floor } = Math
8+
const start = new Node(startTime), finish = new Node(finishTime)
9+
if(finish.compare(start)) finish.hour += 24
10+
let cnt = 0
11+
if(start.hour === finish.hour) {
12+
const r = floor(finish.minute / 15)
13+
const l = ceil(start.minute / 15)
14+
if(l >= r) return 0
15+
return r - l
16+
}
17+
cnt += 4 - ceil(start.minute / 15) + floor(finish.minute / 15)
18+
start.hour++
19+
cnt += (finish.hour - start.hour) * 4
20+
return cnt
1121
};
22+
23+
class Node {
24+
constructor(str) {
25+
this.hour = +str.slice(0, 2)
26+
this.minute = +str.slice(3)
27+
}
28+
compare(node) {
29+
return this.hour === node.hour ? this.minute < node.minute : this.hour < node.hour
30+
}
31+
}

0 commit comments

Comments
 (0)