Leetcode 1344. Angle Between Hands of a Clock

Problem Explanation:

The problem involves calculating the smaller angle formed between the hour hand and the minute hand of a clock at a given time. The time is provided as two separate numbers, the hour and the minute.

Example:

Let's consider an example to better understand the problem. Suppose the hour is 3 and the minutes is 30. The hour hand would be halfway between 3 and 4, and the minute hand would be at 6. The angle between the hour hand and the minute hand would be 75 degrees.

In any given hour, the minute hand makes a full circle (360 degrees), while the hour hand makes 1/12 of a circle (30 degrees). Furthermore, in one minute, the minute hand moves 6 degrees (360 degrees / 60) and the hour hand moves 0.5 degrees (30 degrees / 60). Therefore, after m minutes, the position of the hour hand is (h * 60 + m) * 0.5 and the position of the minute hand is m * 6. The difference between the two represents the absolute angle formed by the hands.

The smaller angle between the two hands can be obtained by ensuring that the calculated angle is <= 180. This can be done by returning min(diff, 360 - diff).

Solution:

Python Solution:

1
2python
3class Solution:
4    def angleClock(self, hour: int, minutes: int) -> float:
5        # Calculate the position of the hour hand
6        hourHand = (hour % 12 + minutes / 60.0) * 30
7        # Calculate the position of the minute hand
8        minuteHand = minutes * 6
9        # Calculate the absolute difference between the hour hand and the minute hand
10        diff = abs(hourHand - minuteHand)
11        # Return the smaller angle formed by the hour hand and the minute hand
12        return min(diff, 360 - diff)

Java Solution:

1
2java
3class Solution {
4  public double angleClock(int hour, int minutes) {
5    double hourHand = (hour % 12 + minutes / 60.0) * 30;
6    double minuteHand = minutes * 6;
7    double diff = Math.abs(hourHand - minuteHand);
8    return Math.min(diff, 360 - diff);
9  }
10}

Javascript Solution:

1
2javascript
3var angleClock = function(hour, minutes) {
4    let hourHand = (hour % 12 + minutes / 60.0) * 30;
5    let minuteHand = minutes * 6;
6    let diff = Math.abs(hourHand - minuteHand);
7    return Math.min(diff, 360 - diff);
8};

C++ Solution:

1
2cpp
3class Solution {
4public:
5    double angleClock(int hour, int minutes) {
6        double hourHand = (hour % 12 + minutes / 60.0) * 30;
7        double minuteHand = minutes * 6;
8        double diff = abs(hourHand - minuteHand);
9        return min(diff, 360 - diff);
10    }
11};

C# Solution:

1
2csharp
3public class Solution {
4    public double AngleClock(int hour, int minutes) {
5        double hourHand = (hour % 12 + minutes / 60.0) * 30;
6        double minuteHand = minutes * 6;
7        double diff = Math.Abs(hourHand - minuteHand);
8        return Math.Min(diff, 360 - diff);
9    }
10}

Problem Analysis:

To solve this problem, we need to first be familiar with how the clock operates. The minute hand moves at a faster pace than the hour hand. For every 360° turn the minute hand turns, the hour hand only moves 30°. This is because the hour hand moves from one hour to the next in the same time the minute hand makes a full circle. This relationship will be helpful in solving the problem.

Another crucial point of consideration is that the angle between the hour hand and the minute hand is always the smaller angle. For instance, if the time was 9 o' clock, the angle between the two hands will be 90° and not the reflex angle of 270°.

Thus, our strategy to solve this problem would be to calculate the angular positions of both the hour hand and the minute hand, take the absolute difference of these positions to initially calculate the angle. Then to ensure we have the smaller angle, we compare the calculated angle with its reflex angle(360° - calculated angle) and return the smaller one.

Let's look at the solutions in the mentioned languages.

PHP Solution:

1
2php
3Class Solution{
4
5    public function angleClock($hour, $minutes){
6        $hourHand = (($hour % 12) + ($minutes / 60.0)) * 30;
7        $minuteHand = $minutes * 6;
8        $diff = abs($hourHand - $minuteHand);
9        return min($diff, 360 - $diff);
10    }
11}

Swift Solution:

1
2swift
3class Solution {
4    func angleClock(_ hour: Int, _ minutes: Int) -> Double {
5        let hourHand = (Double(hour % 12) + Double(minutes) / 60.0) * 30
6        let minuteHand = Double(minutes) * 6
7        let diff = abs(hourHand - minuteHand)
8        return min(diff, 360 - diff)
9    }
10}

Ruby Solution:

1
2ruby
3class Solution
4    def angle_clock(hour, minutes)
5        hour_hand = ((hour % 12 + minutes / 60.0)) * 30
6        minute_hand = minutes * 6
7        diff = (hour_hand - minute_hand).abs
8        return [diff, 360 - diff].min
9    end
10end

Matlab Solution:

1
2matlab
3function angle = angleClock(hour, minutes)
4    hourHand = (mod(hour, 12) + minutes / 60.0) * 30;
5    minuteHand = minutes * 6;
6    diff = abs(hourHand - minuteHand);
7    angle = min(diff, 360 - diff);
8end

Each of these implementations basically follows the same approach in computing the smaller angle between the hour and minutes hands of a clock at a given time. With this understanding and the solutions given, you should be able to comfortably compute the lesser angle between the hands of a clock at any time.


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫