Problem Statement in English
You’re given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand on a clock.
Approach
To solve this problem, we need to calculate the angles of both the hour and minute hands with respect to 12 o’clock and then find the smaller angle between them.
The minute hand moves 6 degrees per minute (360 degrees / 60 minutes), while the hour hand moves 30 degrees per hour (360 degrees / 12 hours) plus an additional 0.5 degrees per minute (30 degrees / 60 minutes).
And we’re done!
Solution in Python
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
v = abs((6 * minutes) - (30 * (0 if hour == 12 else hour ) + 0.5 * minutes))
return min(v, 360 - v)
Complexity
Time: $O(1)$
Since we are performing a constant number of operations to calculate the angle, the time complexity is $O(1)$.Space: $O(1)$
Since we are using only a constant amount of space to store the intermediate variables, the space complexity is $O(1)$.
And we are done.