Leetcode 1344: Angle Between Hands of a Clock

grid47
grid47
Exploring patterns and algorithms
Jun 25, 2024 4 min read

Given an hour and minute value, return the smaller angle (in degrees) formed between the hour and minute hands of a clock.
Problem
Approach
Steps
Complexity
Input: The input consists of two integers representing the hour (1 to 12) and the minute (0 to 59).
Example: hour = 6, minutes = 45
Constraints:
• 1 <= hour <= 12
• 0 <= minutes <= 59
Output: Return the smaller angle in degrees between the hour and minute hands of a clock.
Example: For hour = 6, minutes = 45, the output will be 22.5.
Constraints:
• The answer should be within 10^-5 of the actual value.
Goal: Calculate the angle between the hour and minute hands of a clock and return the smaller angle.
Steps:
• 1. Calculate the angle of the minute hand based on the number of minutes.
• 2. Calculate the angle of the hour hand, adjusting for the current minute.
• 3. Find the absolute difference between the two angles.
• 4. Return the smaller angle (either the absolute difference or 360 - difference).
Goal: The solution should handle all valid clock inputs efficiently.
Steps:
• The hour value is between 1 and 12.
• The minute value is between 0 and 59.
Assumptions:
• The given time values are always valid.
• The hour value is always between 1 and 12, and the minute value is between 0 and 59.
Input: Example 1: hour = 6, minutes = 45
Explanation: At 6:45, the minute hand is at the 9th hour mark, and the hour hand is at 6:45, creating an angle of 22.5 degrees.

Input: Example 2: hour = 9, minutes = 15
Explanation: At 9:15, the minute hand is at the 3rd hour mark, and the hour hand is at 9:15, forming an angle of 52.5 degrees.

Input: Example 3: hour = 10, minutes = 0
Explanation: At 10:00, the angle between the two hands is exactly 60 degrees, as the minute hand is at 12 and the hour hand is at 10.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus