Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Angle Between Hands of a Clock

Posted on August 5, 2019July 26, 2020 by braindenny

Angle Between Hands of a Clock



Similar Problems:

  • CheatSheet: LeetCode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #geometry, #math

Given two numbers, hour and minutes. Return the smaller angle (in sexagesimal units) formed between the hour and the minute hand.

Example 1:
LeetCode: Angle Between Hands of a Clock

Input: hour = 12, minutes = 30
Output: 165

Example 2:
LeetCode: Angle Between Hands of a Clock

Input: hour = 3, minutes = 30
Output: 75

Example 3:
LeetCode: Angle Between Hands of a Clock

Input: hour = 3, minutes = 15
Output: 7.5

Example 4:

Input: hour = 4, minutes = 50
Output: 155

Example 5:

Input: hour = 12, minutes = 0
Output: 0

Constraints:

  • 1 <= hour <= 12
  • 0 <= minutes <= 59
  • Answers within 10^-5 of the actual value will be accepted as correct.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
## https://code.dennyzhang.com/angle-between-hands-of-a-clock
## Basic Ideas: math
##
##  When hour change by one, it has 30 degrees
##  When minute change by one, it has 6 degrees
##  The change of minutes adjust hours' change
##     When hour change by one, it means minutes change by 60
##
##  Change from 00:00 to hour:minute
##
##    d = abs(30*hour + 30*(minutes/60) - minutes*6)
##    min(d, 360-d)
## Complexity: Time O(1), Space O(1)
class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        d = abs(30*(hour%12) + 30*(minutes/60) - minutes*6)
        return min(d, 360-d)
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #math, geometry

Post navigation

LeetCode: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
LeetCode: Jump Game IV

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.