MediumNo. 1344

Angle Between Hands of a Clock — LeetCode 1344 Python Solution

Topics
Math
Reading time
2 min

The problem

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

Input
hour = 12, minutes = 30
Output
165

Python solution

PythonSolution
class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        h = 30 * hour + 0.5 * minutes
        m = 6 * minutes
        diff = abs(h - m)
        return min(diff, 360 - diff)

Complexity

Time
O(n) or O(1)
Space · auxiliary
O(1)

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1344. Angle Between Hands of a Clock is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0002Add Two NumbersMedium
0007Reverse IntegerMedium
0009Palindrome NumberEasy
0043Multiply StringsMedium
0048Rotate ImageMedium
0050Pow(x, n)Medium

Frequently asked questions

How hard is LeetCode 1344. Angle Between Hands of a Clock?

LeetCode 1344. Angle Between Hands of a Clock is rated Medium on LeetCode.

What topics does LeetCode 1344. Angle Between Hands of a Clock cover?

LeetCode 1344. Angle Between Hands of a Clock is tagged Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.