본문 바로가기

카테고리 없음

[스터디] 알고리즘#4 - 동영상 재생기

동영상 재생기 문제 - 이것도 시간(60진법 -> 10진법) 변환잘하고, 주어진 조건 잘 계산한다음,

다시 10진번 -> 60진법으로만 변환해주면 쉽게 푸는 문제이다.

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/340213

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

class Solution {
    fun solution(video_len: String, pos: String, op_start: String, op_end: String, commands: Array<String>): String {
        var answer: String = ""

        val iVideoLen = video_len.split(":").let { it[0].toInt() * 60 + it[1].toInt() }
        var iPos = pos.split(":").let { it[0].toInt() * 60 + it[1].toInt() }
        val iOpStart = op_start.split(":").let { it[0].toInt() * 60 + it[1].toInt() }
        val iOpEnd = op_end.split(":").let { it[0].toInt() * 60 + it[1].toInt() }

        if (iPos in iOpStart..iOpEnd ) {
            iPos = iOpEnd
        }

        commands.forEach {
            when(it) {
                "next" -> {
                    iPos += 10
                    if (iPos > iVideoLen - 10) iPos = iVideoLen
                }
                "prev" -> {
                    iPos -= 10
                    if (iPos < 10) iPos = 0
                }
            }

            if (iPos in iOpStart..iOpEnd ) {
                iPos = iOpEnd
            }
        }


        val h = iPos / 60
        val m = iPos % 60
        answer = String.format("%02d:%02d", h, m)
        return answer
    }
}