#1344

Angle Between Hands of a Clock

Medium
MathMathematical calculationsGeometry
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(1)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

The optimal solution is essentially the same as the brute force approach but emphasizes the efficiency of direct calculations without unnecessary steps. It leverages the properties of angles on a circle.

⚙️

Algorithm

4 steps
  1. 1Step 1: Calculate the angle of the minute hand: angle_minute = minutes * 6.
  2. 2Step 2: Calculate the angle of the hour hand, considering both the hour and the contribution from the minutes: angle_hour = (hour % 12) * 30 + (minutes / 60) * 30.
  3. 3Step 3: Calculate the absolute difference between the two angles: angle_difference = abs(angle_hour - angle_minute).
  4. 4Step 4: Return the smaller angle: return min(angle_difference, 360 - angle_difference).
solution.py5 lines
1def angleClock(hour, minutes):
2    angle_minute = minutes * 6
3    angle_hour = (hour % 12) * 30 + (minutes / 60) * 30
4    angle_difference = abs(angle_hour - angle_minute)
5    return min(angle_difference, 360 - angle_difference)

Complexity note: The optimal solution also runs in O(1) time and space because it performs a constant number of calculations.

  • 1The minute hand moves 6 degrees per minute, while the hour hand moves 30 degrees per hour plus an additional 0.5 degrees for each minute.
  • 2Understanding how both hands move relative to each other is crucial for calculating the angle.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.