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.

  1. Let us find the place, where hour hand is. First, whe have 12 hours in total, for 360 degrees, that means 30 degrees per hour. Also, for every 60 minutes, our hour hand rotated by 1 hour, that is 30 degrees, so for every minute, it is rotated by 0.5 degrees. So, final place for hour hand is 30*hour + 0.5*minutes
  2. Let us find the place, where minute hand is: every 60 minutes minute hand makes full rotation, that means we have 6 degrees for each minute.
  3. 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