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 def angleClock(self, hour: int, minutes: int) -> float:
3 minute_angle = minutes * 6
4 hour_angle = (hour % 12) * 30 + minutes * 0.5
5 angle = abs(minute_angle - hour_angle)
6 return min(angle, 360 - angle)
7
8sol = Solution()
9print(f"{sol.angleClock(12, 30):.5f}") # Output: 165.00000
10print(f"{sol.angleClock(3, 30):.5f}") # Output: 75.00000
11print(f"{sol.angleClock(3, 15):.5f}") # Output: 7.50000
Python solution uses the same arithmetic logic to calculate the angles and returns the minimum of the calculated angle and its complement (360 minus the angle). The formatted string ensures precision in printing the result.
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#include <cmath>
class Solution {
public:
double angleClock(int hour, int minutes) {
int total_minutes = hour * 60 + minutes;
double minute_angle = (total_minutes % 60) * 6;
double hour_angle = (total_minutes % 720) * 0.5;
double angle = fabs(minute_angle - hour_angle);
return angle > 180 ? 360 - angle : angle;
}
};
int main() {
Solution sol;
std::cout << std::fixed << sol.angleClock(12, 30) << std::endl; // Output: 165.00000
std::cout << std::fixed << sol.angleClock(3, 30) << std::endl; // Output: 75.00000
std::cout << std::fixed << sol.angleClock(3, 15) << std::endl; // Output: 7.50000
return 0;
}
In C++, this solution similarly leverages calculation of the total minutes elapsed. It computes angular positions of clock hands proportional to complete cycles of movement, producing the minimal angular separation between hands.