본문 바로가기
Algorithm/LeetCode

💯 Daily LeetCode Challenge Day_14 - Angle between hands of a clock

by HaningYa 2020. 7. 14.
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

댓글