ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Contest 246] 1904. The Number of ... / Swift
    프로그래밍/코딩테스트 2021. 6. 21. 01:04

    1904. The Number of Full Rounds You Have Played / Swift

    [문제 보기]

    더보기

    1904. The Number of Full Rounds You Have Played

    • User Accepted:3829
    • User Tried:4637
    • Total Accepted:3987
    • Total Submissions:10719
    • Difficulty:Medium

    A new online video game has been released, and in this video game, there are 15-minute rounds scheduled every quarter-hour period. This means that at HH:00, HH:15, HH:30 and HH:45, a new round starts, where HH represents an integer number from 00 to 23. A 24-hour clock is used, so the earliest time in the day is 00:00 and the latest is 23:59.

    Given two strings startTime and finishTime in the format "HH:MM" representing the exact time you started and finished playing the game, respectively, calculate the number of full rounds that you played during your game session.

    • For example, if startTime = "05:20" and finishTime = "05:59" this means you played only one full round from 05:30 to 05:45. You did not play the full round from 05:15 to 05:30 because you started after the round began, and you did not play the full round from 05:45 to 06:00 because you stopped before the round ended.

    If finishTime is earlier than startTime, this means you have played overnight (from startTime to the midnight and from midnight to finishTime).

    Return the number of full rounds that you have played if you had started playing at startTime and finished at finishTime.

     

    Example 1:

    Input: startTime = "12:01", finishTime = "12:44" Output: 1 Explanation: You played one full round from 12:15 to 12:30. You did not play the full round from 12:00 to 12:15 because you started playing at 12:01 after it began. You did not play the full round from 12:30 to 12:45 because you stopped playing at 12:44 before it ended.

    Example 2:

    Input: startTime = "20:00", finishTime = "06:00" Output: 40 Explanation: You played 16 full rounds from 20:00 to 00:00 and 24 full rounds from 00:00 to 06:00. 16 + 24 = 40.

    Example 3:

    Input: startTime = "00:00", finishTime = "23:59" Output: 95 Explanation: You played 4 full rounds each hour except for the last hour where you played 3 full rounds.

     

    Constraints:

    • startTime and finishTime are in the format HH:MM.
    • 00 <= HH <= 23
    • 00 <= MM <= 59
    • startTime and finishTime are not equal.

     

    https://leetcode.com/problems/the-number-of-full-rounds-you-have-played/

     


    4점짜리 2번문제였다.

     

    start시간은 이후에 다가오는 가장빠른 시작시간으로 조정하고 ( 11:23 -> 11:30),

    finish시간은 직전에 가장빠른 종료시간으로 조정해서 ( 11:23 -> 11:15) 반복된 횟수를 구했다.

    그리고 finish가 start보다 작아질 때( 23:59 -> 00:00  경계)는 finish에 하루만큼 시간을 추가로 더해서 경과시간을 구할 때 (finish - start) 오류가 없도록 했다.

     

    class Solution {
        func numberOfRounds(_ startTime: String, _ finishTime: String) -> Int {
            var start = startTime.split(separator: ":").map{Int(String($0))!}
            var finish = finishTime.split(separator: ":").map{Int(String($0))!}
            
            var temp = start[1] + 14
            if temp >= 60 { start[0] += 1; temp = 0 }
            start[1] = (temp / 15) * 15
            finish[1] = (finish[1] / 15) * 15
            
            let d1 = start[0] * 60 + start[1]
            var d2 = finish[0] * 60 + finish[1]
            if d1 > d2 {
                d2 += 24 * 60
            }
            
            let ans = (d2 - d1) / 15
            
            return ans
        }
    }

    그러나 이렇게 작성하면 입력값이 12:10, 12:14로 주어질때 각각 12:15, 12:00 으로 바뀌는데 이부분을 처리하지 못했다.

     

    두번째 작성한 코드는 방금 말한 nextStart가  previousFinish보다 작아지는 경우 최소값이 0으로 나오게 수정한 코드이다.

    최종 작성한 코드:

    class Solution {
        func numberOfRounds(_ startTime: String, _ finishTime: String) -> Int {
            var start = startTime.split(separator: ":").map{Int(String($0))!}
            var finish = finishTime.split(separator: ":").map{Int(String($0))!}
            
            let totalStart = 60 * start[0] + start[1]
            let totalFinish = 60 * finish[0] + finish[1]
            
            let nextStart = ((totalStart + 14) / 15) * 15
            let previousFinish = (totalFinish / 15) * 15
            
            if totalStart < totalFinish {
                return max(0, (previousFinish - nextStart) / 15)
            }
            
            return (previousFinish + 60 * 24 - nextStart) / 15
        }
    }
    반응형

    댓글

Designed by Tistory.