Angle Between Hands of a Clock

Similar Problems:

- CheatSheet: LeetCode For Code Interview
- CheatSheet: Common Code Problems & Follow-ups
- Tag: #geometry, #math

Given two numbers, hour and minutes. Return the smaller angle (in sexagesimal units) formed between the hour and the minute hand.

Input: hour = 12, minutes = 30 Output: 165

Input: hour = 3, minutes = 30 Output: 75

Input: hour = 3, minutes = 15 Output: 7.5

Example 4:

Input: hour = 4, minutes = 50 Output: 155

Example 5:

Input: hour = 12, minutes = 0 Output: 0

Constraints:

- 1 <= hour <= 12
- 0 <= minutes <= 59
- Answers within 10^-5 of the actual value will be accepted as correct.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution:

## https://code.dennyzhang.com/angle-between-hands-of-a-clock ## Basic Ideas: math ## ## When hour change by one, it has 30 degrees ## When minute change by one, it has 6 degrees ## The change of minutes adjust hours' change ## When hour change by one, it means minutes change by 60 ## ## Change from 00:00 to hour:minute ## ## d = abs(30*hour + 30*(minutes/60) - minutes*6) ## min(d, 360-d) ## Complexity: Time O(1), Space O(1) class Solution: def angleClock(self, hour: int, minutes: int) -> float: d = abs(30*(hour%12) + 30*(minutes/60) - minutes*6) return min(d, 360-d)