Leetcode 1344: Angle between hands of a clock
2 min readFeb 10, 2020
In this Leetcode problem, we look to find the angle between the minute
and the hour
of a clock.
In that case, this is more a Math than computer science problem. So let’s put our geometry hat and calculate the angle between the origin and the hour hand:
hour angle:alpha = (minutes spent) * 360.0 / (total minutes for full circle)
= ((hour * 60) + minutes) * 360.0 / 12 * 60
= ((hour * 60) + minutes) / 2.0
Result is (% 360.0) since the number of hours can be 12+ and we want an angle between 0 and 360.
Then the minutes angle:
minutes angle:beta = minutes * 360.0 / (total minutes for full circle)
= minutes * 360.0 / 60.0
= minutes * 6.0
Then the (positive) angle formed by the 2 hands is:
angle = Math.abs(beta — alpha)
Then, of course, we need to calculate the other positive angle to get from one hand to the other, then:
result = Math.min(angle, 360.0 - angle)
Here is the code, making the calculations I detailed above:
class Solution {
public double angleClock(int hour, int minutes) {
double alpha = ((hour * 60 + minutes) / 2.0) % 360.0;
double beta…