Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1344. Angle Between Hands of a Clock - Leetcode Solution

Problem Description

The LeetCode problem "Angle Between Hands of a Clock" asks you to calculate the smallest angle in degrees between the hour and minute hands of an analog clock, given a specific time.

You are given two integers:

  • hour: an integer between 1 and 12 (inclusive) representing the hour hand
  • minutes: an integer between 0 and 59 (inclusive) representing the minute hand
Your task is to determine the smallest angle (in degrees) between the two hands at this time.

Constraints:

  • 1 ≤ hour ≤ 12
  • 0 ≤ minutes ≤ 59
  • The answer should be a floating-point number (can have decimals)
  • Return the smallest possible angle (i.e., if the angle is greater than 180°, subtract it from 360°)

Thought Process

To solve this problem, let's first visualize how the hands of a clock move:

  • The minute hand moves 360° in 60 minutes, so each minute advances it by 6°.
  • The hour hand moves 360° in 12 hours, so each hour advances it by 30°. However, it also moves as the minutes advance (it doesn't "jump" each hour).
  • Therefore, as the minutes increase, the hour hand also moves proportionally: each minute moves the hour hand by 0.5° (since 30°/60 = 0.5° per minute).
The brute-force approach might be to simulate the movement of both hands, but we can directly compute their positions using these observations.

The challenge is to correctly calculate both hand positions and then find the smallest angle between them.

Solution Approach

  • Step 1: Calculate the angle of the hour hand.
    • The hour hand moves 30° for each hour and 0.5° for each minute.
    • Formula: hour_angle = (hour % 12) * 30 + (minutes * 0.5)
  • Step 2: Calculate the angle of the minute hand.
    • The minute hand moves 6° for each minute.
    • Formula: minute_angle = minutes * 6
  • Step 3: Find the absolute difference between the two angles.
    • angle = abs(hour_angle - minute_angle)
  • Step 4: Return the smallest angle.
    • If the angle is more than 180°, subtract it from 360° to get the smaller equivalent angle.
    • return min(angle, 360 - angle)

This approach is efficient and direct, using only a few arithmetic operations.

Example Walkthrough

Let's consider the input hour = 3, minutes = 15.

  • Hour hand:
    • hour_angle = (3 % 12) * 30 + (15 * 0.5) = 90 + 7.5 = 97.5°
  • Minute hand:
    • minute_angle = 15 * 6 = 90°
  • Angle between:
    • angle = abs(97.5 - 90) = 7.5°
    • Since 7.5° is less than 180°, that's our answer.

For hour = 12, minutes = 30:

  • Hour angle: (12 % 12) * 30 + (30 * 0.5) = 0 + 15 = 15°
  • Minute angle: 30 * 6 = 180°
  • Angle: abs(15 - 180) = 165°
  • Since 165° is less than 180°, that's our answer.

Time and Space Complexity

  • Brute-force approach:
    • If we tried to simulate every minute, time complexity would be O(1) anyway, since the input is always a single time.
    • But this is unnecessary; direct calculation is much better.
  • Optimized approach (the one above):
    • Time Complexity: O(1) — Only a few arithmetic operations, regardless of input.
    • Space Complexity: O(1) — Only a constant number of variables are used.

Summary

To solve the "Angle Between Hands of a Clock" problem, we use the fact that the hour hand and minute hand move at constant, predictable rates. By calculating the position of each hand in degrees, finding the absolute difference, and returning the smaller angle (if necessary by subtracting from 360°), we arrive at a clean, efficient, and accurate solution. The key insight is to model the positions mathematically rather than simulating the clock's movement.

Code Implementation

class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        hour_angle = (hour % 12) * 30 + (minutes * 0.5)
        minute_angle = minutes * 6
        angle = abs(hour_angle - minute_angle)
        return min(angle, 360 - angle)
      
class Solution {
public:
    double angleClock(int hour, int minutes) {
        double hour_angle = (hour % 12) * 30 + (minutes * 0.5);
        double minute_angle = minutes * 6;
        double angle = fabs(hour_angle - minute_angle);
        return std::min(angle, 360 - angle);
    }
};
      
class Solution {
    public double angleClock(int hour, int minutes) {
        double hourAngle = (hour % 12) * 30 + (minutes * 0.5);
        double minuteAngle = minutes * 6;
        double angle = Math.abs(hourAngle - minuteAngle);
        return Math.min(angle, 360 - angle);
    }
}
      
var angleClock = function(hour, minutes) {
    let hourAngle = (hour % 12) * 30 + (minutes * 0.5);
    let minuteAngle = minutes * 6;
    let angle = Math.abs(hourAngle - minuteAngle);
    return Math.min(angle, 360 - angle);
};