Sponsored
Sponsored
This approach involves calculating the angles made by the hour hand and the minute hand with respect to 12:00 and then finding the smallest angle between them. The minute hand moves 6 degrees per minute (360 degrees / 60 minutes), and the hour hand moves 30 degrees per hour (360 degrees / 12 hours) plus an additional 0.5 degrees per minute. The difference between these angles gives us the desired result.
Time Complexity: O(1) - The computation involves a fixed number of arithmetic operations.
Space Complexity: O(1) - No additional space is required beyond fixed-size variables.
1class Solution {
2 angleClock(hour, minutes) {
3 let minute_angle = minutes * 6;
4 let hour_angle = (hour % 12) * 30 + minutes * 0.5;
5 let angle = Math.abs(minute_angle - hour_angle);
6 return angle > 180 ? 360 - angle : angle;
7 }
8}
9
10const sol = new Solution();
11console.log(sol.angleClock(12, 30).toFixed(5)); // Output: 165.00000
12console.log(sol.angleClock(3, 30).toFixed(5)); // Output: 75.00000
13console.log(sol.angleClock(3, 15).toFixed(5)); // Output: 7.50000
JavaScript solution employs OOP principles, creating a method to perform computations to find the angle values based on minute and hour inputs. The smallest angle is returned after comparisons. Outputs are formatted using toFixed for precision.
This alternative approach converts the movement of the clock hands into their equivalent rotations, effectively translating this into angles. The goal is to determine the position of both hands as angles relative to the 12 o'clock position and compute the minimal angular difference.
Time Complexity: O(1) - Fixed-time operations based on input.
Space Complexity: O(1) - Uses a constant number of variables.
1
The C solution adjusts the total time into minutes, facilitating the computation of angles directly based on the total elapsed minutes. This methodology leverages modular arithmetic to find positions relative to whole-hour and half-hour rotations, ultimately returning the minimal calculated angle.