Algorithm/LeetCode

๐Ÿ’ฏ Daily LeetCode Challenge Day_14 - Angle between hands of a clock

HaningYa 2020. 7. 14. 17:50
728x90

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com


๋ฌธ์ œ

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

Example 1:

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

Example 2:

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

Example 3:

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.

๋ถ„์นจ์— ๋”ฐ๋ผ ์‹œ์นจ๋„ ๋ฐ”๋€๋‹ค๋Š” ๊ฒƒ๊ณผ 360๋„๋Š” 0๋„๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ, ๋‘ ์นจ์˜ ์ž‘์€ ๊ฐ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š”๊ฒƒ๋งŒ ์ƒ๊ฐํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

class Solution {
    func angleClock(_ hour: Int, _ minutes: Int) -> Double {
        var mAngle : Double = 6*Double(minutes)
        var hAngle : Double = ( (30*Double(hour)) + (30*Double(minutes)/60) )
        if hAngle >= 360 { hAngle -= 360 }
        var minAngle = min(abs(hAngle-mAngle),abs(mAngle-hAngle))
        return min(abs(360-minAngle),minAngle)
    }
}

 

 

 

728x90