[
math
]
Leetcode 1344. Angle Between Hands of a Clock
https://leetcode.com/problems/angle-between-hands-of-a-clock
To solve this problem we need to understand the speeds of Hands of a clock.
- Let us find the place, where hour hand is. First, whe have
12
hours in total, for360
degrees, that means30
degrees per hour. Also, for every60
minutes, our hour hand rotated by1
hour, that is30
degrees, so for every minute, it is rotated by0.5
degrees. So, final place for hour hand is30*hour + 0.5*minutes
- Let us find the place, where minute hand is: every
60
minutes minute hand makes full rotation, that means we have6
degrees for each minute. - Finally, we evaluate absolute difference between these two numbers, and if angle is more than
180
degrees, we return complementary angle.
Complexity: time and space is O(1)
, we just use some mathematical formulae.
class Solution:
def angleClock(self, hour, minutes):
H_place = 30*hour + 0.5*minutes
M_place = 6*minutes
diff = abs(H_place - M_place)
return diff if diff <= 180 else 360 - diff
Oneliner We can write this as oneliner as well:
return min(abs(30*hour-5.5*minutes),360-abs(30*hour-5.5*minutes))
If you like the solution, you can upvote it on leetcode discussion section: Problem 1344