Angle Between Hands of a Clock
Description:โ
Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.
Example 1:
Input: hour = 12, minutes = 30
Output: 165
Example 2:
Input: hour = 3, minutes = 30
Output: 75
Example 3:
Input: hour = 3, minutes = 15
Output: 7.5
Approaches:โ
1. Mathematical Approach (Relative Speeds)โ
To find the angle between the hands, we calculate the exact position (in degrees) of both the minute and hour hands relative to the 12:00 position, and then find the absolute difference between them.
- Minute Hand: The minute hand moves in minutes. Therefore, it moves at a speed of per minute.
- Hour Hand: The hour hand moves in hours ( minutes). Therefore, it moves at a speed of per minute. It also shifts based on the current hour.
- Find the Difference: Subtract the two angles and take the absolute value.
- Find the Smaller Angle: A clock is . If the difference is greater than , the smaller angle will be on the other side. Return the minimum of the difference and minus the difference.
- Time Complexity: because the calculation relies on a few basic arithmetic operations, regardless of the input size.
- Space Complexity: because we only use a few variables to store the angles, requiring constant extra space.
Solutions:โ
C++
class Solution {
public:
double angleClock(int hour, int minutes) {
double minute_angle = minutes * 6.0;
double hour_angle = (hour % 12) * 30.0 + minutes * 0.5;
double diff = std::abs(hour_angle - minute_angle);
return std::min(diff, 360.0 - diff);
}
};
Java
class Solution {
public double angleClock(int hour, int minutes) {
double minuteAngle = minutes * 6.0;
double hourAngle = (hour % 12) * 30.0 + minutes * 0.5;
double diff = Math.abs(hourAngle - minuteAngle);
return Math.min(diff, 360.0 - diff);
}
}
Python
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
minute_angle = minutes * 6
hour_angle = (hour % 12) * 30 + minutes * 0.5
diff = abs(hour_angle - minute_angle)
return min(diff, 360 - diff)
JavaScript
/**
* @param {number} hour
* @param {number} minutes
* @return {number}
*/
const angleClock = function(hour, minutes) {
const minuteAngle = minutes * 6;
const hourAngle = (hour % 12) * 30 + minutes * 0.5;
const diff = Math.abs(hourAngle - minuteAngle);
return Math.min(diff, 360 - diff);
};
Telemetry Integration
Completed working through this block? Sync progress to workspace.