LeetCode link: 1344. Angle Between Hands of a Clock, difficulty: Medium.
Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.
Answers within 10^-5 of the actual value will be accepted as correct.
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
Constraints:
1 <= hour <= 120 <= minutes <= 59
Topics
Hint 1
The tricky part is determining how the minute hand affects the position of the hour hand.
Hint 2
Calculate the angles separately then find the difference.
Intuition
To find the angle between the hour and minute hands, we can calculate the angle of each hand relative to the 12 o'clock position (0 degrees) and then find the absolute difference between these two angles.
First, let's look at the minute hand:
The clock has 360 degrees and 60 minutes. Therefore, the minute hand moves 360 / 60 = 6 degrees per minute.
So, the angle of the minute hand is simply minutes * 6.
Next, let's consider the hour hand:
The clock has 360 degrees and 12 hours. Thus, the hour hand moves 360 / 12 = 30 degrees per hour.
However, the hour hand also moves slightly as the minutes pass! In 60 minutes, it moves 30 degrees, which means it moves 30 / 60 = 0.5 degrees per minute.
So, the angle of the hour hand is (hour % 12) * 30 + minutes * 0.5. (We use hour % 12 because 12 o'clock is the same starting position as 0).
Finally, we calculate the absolute difference between the two angles: diff = abs(hour_angle - minute_angle).
Since the problem asks for the smaller angle, and the hands divide the clock into two angles that sum to 360 degrees, we return the minimum of diff and 360 - diff.
👋 Hi, I'm the developer behind coder.id — Build your programmer brand. Create your portfolio, resume, and blog in minutes, and land your dream offer!
Step-by-Step Solution
- Calculate the minute hand angle:
minute_angle = minutes * 6.0. - Calculate the hour hand angle:
hour_angle = (hour % 12) * 30.0 + minutes * 0.5. - Find the absolute difference:
diff = abs(hour_angle - minute_angle). - Return the smaller angle:
min(diff, 360.0 - diff).
Complexity
Time complexity
O(1)
Space complexity
O(1)
Explanation
- Time Complexity:
O(1), because we only perform a few basic arithmetic operations. - Space Complexity:
O(1), as we only use a few variables to store the angles.
Python #
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
# Calculate the angle of the minute hand
minute_angle = minutes * 6
# Calculate the angle of the hour hand
hour_angle = (hour % 12) * 30 + minutes * 0.5
# Find the absolute difference between the two angles
diff = abs(hour_angle - minute_angle)
# Return the smaller angle
return min(diff, 360 - diff)
Java #
class Solution {
public double angleClock(int hour, int minutes) {
// Calculate the angle of the minute hand
double minuteAngle = minutes * 6.0;
// Calculate the angle of the hour hand
double hourAngle = (hour % 12) * 30.0 + minutes * 0.5;
// Find the absolute difference between the two angles
double diff = Math.abs(hourAngle - minuteAngle);
// Return the smaller angle
return Math.min(diff, 360.0 - diff);
}
}
JavaScript #
/**
* @param {number} hour
* @param {number} minutes
* @return {number}
*/
var angleClock = function(hour, minutes) {
// Calculate the angle of the minute hand
const minuteAngle = minutes * 6;
// Calculate the angle of the hour hand
const hourAngle = (hour % 12) * 30 + minutes * 0.5;
// Find the absolute difference between the two angles
const diff = Math.abs(hourAngle - minuteAngle);
// Return the smaller angle
return Math.min(diff, 360 - diff);
};
C++ #
class Solution {
public:
double angleClock(int hour, int minutes) {
// Calculate the angle of the minute hand
double minute_angle = minutes * 6.0;
// Calculate the angle of the hour hand
double hour_angle = (hour % 12) * 30.0 + minutes * 0.5;
// Find the absolute difference between the two angles
double diff = abs(hour_angle - minute_angle);
// Return the smaller angle
return min(diff, 360.0 - diff);
}
};
C# #
public class Solution {
public double AngleClock(int hour, int minutes) {
// Calculate the angle of the minute hand
double minuteAngle = minutes * 6.0;
// Calculate the angle of the hour hand
double hourAngle = (hour % 12) * 30.0 + minutes * 0.5;
// Find the absolute difference between the two angles
double diff = Math.Abs(hourAngle - minuteAngle);
// Return the smaller angle
return Math.Min(diff, 360.0 - diff);
}
}
Go #
import "math"
func angleClock(hour int, minutes int) float64 {
// Calculate the angle of the minute hand
minuteAngle := float64(minutes) * 6.0
// Calculate the angle of the hour hand
hourAngle := float64(hour % 12) * 30.0 + float64(minutes) * 0.5
// Find the absolute difference between the two angles
diff := math.Abs(hourAngle - minuteAngle)
// Return the smaller angle
return math.Min(diff, 360.0 - diff)
}
Ruby #
# @param {Integer} hour
# @param {Integer} minutes
# @return {Float}
def angle_clock(hour, minutes)
# Calculate the angle of the minute hand
minute_angle = minutes * 6.0
# Calculate the angle of the hour hand
hour_angle = (hour % 12) * 30.0 + minutes * 0.5
# Find the absolute difference between the two angles
diff = (hour_angle - minute_angle).abs
# Return the smaller angle
[diff, 360.0 - diff].min
end
Rust #
impl Solution {
pub fn angle_clock(hour: i32, minutes: i32) -> f64 {
// Calculate the angle of the minute hand
let minute_angle = (minutes as f64) * 6.0;
// Calculate the angle of the hour hand
let hour_angle = ((hour % 12) as f64) * 30.0 + (minutes as f64) * 0.5;
// Find the absolute difference between the two angles
let diff = (hour_angle - minute_angle).abs();
// Return the smaller angle
diff.min(360.0 - diff)
}
}
Kotlin #
import kotlin.math.*
class Solution {
fun angleClock(hour: Int, minutes: Int): Double {
// Calculate the angle of the minute hand
val minuteAngle = minutes * 6.0
// Calculate the angle of the hour hand
val hourAngle = (hour % 12) * 30.0 + minutes * 0.5
// Find the absolute difference between the two angles
val diff = abs(hourAngle - minuteAngle)
// Return the smaller angle
return min(diff, 360.0 - diff)
}
}
Swift #
class Solution {
func angleClock(_ hour: Int, _ minutes: Int) -> Double {
// Calculate the angle of the minute hand
let minuteAngle = Double(minutes) * 6.0
// Calculate the angle of the hour hand
let hourAngle = Double(hour % 12) * 30.0 + Double(minutes) * 0.5
// Find the absolute difference between the two angles
let diff = abs(hourAngle - minuteAngle)
// Return the smaller angle
return min(diff, 360.0 - diff)
}
}
Other languages
Welcome to contribute code to GitHub repo -> 1344. Angle Between Hands of a Clock. Thanks!Level Up Your Developer Identity
- 📄 Resume, Portfolio & Blog
- 🌐 Custom Domain Binding Supported
- ✨ Premium Subdomain:
https://yourname.coder.id