포스트

프로그래머스 1. 달리기 경주

얀에서는 매년 달리기 경주가 열립니다. 해설진들은 선수들이 자기 바로 앞의 선수를 추월할 때 추월한 선수의 이름을 부릅니다. 예를 들어 1등부터 3등까지 “mumu”, “soe”, “poe” 선수들이 순서대로 달리고 있을 때, 해설진이 “soe”선수를 불렀다면 2등인 “soe” 선수가 1등인 “mumu” 선수를 추월했다는 것입니다. 즉 “soe” 선수가 1등, “mumu” 선수가 2등으로 바뀝니다. 선수들의 이름이 1등부터 현재 등수 순서대로 담긴 문자열 배열 players와 해설진이 부른 이름을 담은 문자열 배열 callings가 매개변수로 주어질 때, 경주가 끝났을 때 선수들의 이름을 1등부터 등수 순서대로 배열에 담아 return 하는 solution 함수를 완성해주세요.

1차 시도

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func solution(_ players:[String], _ callings:[String]) -> [String] {
    var result: [String]
    result = players
    for i in 0...callings.count - 1 {
        for j in 0...result.count - 1 {
            if callings[i] != result[j] {
            } else if callings[i] == result[j] {
                result[j] = result[j - 1]
                result[j - 1] = callings[i]
            }
        }
    }
    return result
}

결과

image 짧은 테스트는 통과하지만, 긴 테스트에서 시간초과가 뜨고 만다. 62.5점짜리 함수다.

왜 시간초과가 뜨는가?

playerscallings배열이 길어질수록, callingsplayers 배열에서 자신이 언제 호명되는지, 검색하기 위해 비교하는 시행횟수 자체가 기하급수적으로 길어진다.

이중포문을 사용하는 이상은, 이 검색의 시행횟수를 줄이는 수밖에 없다.

그렇다면callings에 호명되는 사람 이름이, players에서 몇 번째에 위치하는지 기억하게 할 수 있다면 어떨까?

한 번 검색한 이후로는, 검색하기 위해서 비교하는 시행횟수를 스킵할 수 있을지도 모른다.

그렇다면, 자신의 위치를 기억하게 하는 콜렉션은 어떤 자료형을 사용하면 될까?

[String : Int]라면 가능할지 모른다.

2차 시도

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func solution(_ players:[String], _ callings:[String]) -> [String] {
    var address: [String : Int]
    address = [:]
    var result: [String]
    result = players		
    for i in 0...callings.count - 1 {
        if (address[(callings[i])] != nil) {
            var calledAddress = address[(callings[i])]!
            result[calledAddress] = result[calledAddress - 1]
            address[result[calledAddress - 1]] = calledAddress
            result[calledAddress - 1] = callings[i]
            address[(callings[i])] = calledAddress - 1
        } else {
            for j in 0...result.count - 1 {
                if callings[i] != result[j] {
                } else if callings[i] == result[j] {
                    result[j] = result[j - 1]
                    result[j - 1] = callings[i]
                    address[callings[i]] = j - 1
                    address[result[j]] = j
                }
            }
        }
    }
    print(address)
    return result
}

결과

image

딕셔너리를 이용해 위치를 기억하게 한다는 아이디어는 유효했다. 실행속도가 눈에 띄게 줄었다. 하지만 여전히 네 개의 테스트에서 시간 초과가 출력된다. 75점짜리 함수였다. 이제 뭘 할 수 있을까? players를 통해서 선수와 등수 배열이 주어진다. 최초 딕셔너리는 이것을 통해 탐색 없이 만들 수 있다.

3차 시도

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
func solution2(_ players:[String], _ callings:[String]) -> [String] {
    var address: [String : Int]
    address = [:]
    for k in 0...players.count - 1 {
        address[players[k]] = k
    }
    var result: [String]
    result = players
    for i in 0...callings.count - 1 {
        if (address[(callings[i])] != nil) {
            var calledAddress = address[(callings[i])]! 
            result[calledAddress] = result[calledAddress - 1]
            address[result[calledAddress - 1]] = calledAddress
            result[calledAddress - 1] = callings[i]
            address[(callings[i])] = calledAddress - 1
        } else if i > 0 && (address[(callings[i - 1])] != nil) {
                var calledAddressAhead = address[(callings[i - 1])]! 
                result[i] = result[calledAddressAhead]
                address[result[calledAddressAhead]] = i
                result[calledAddressAhead] = callings[i]
                address[(callings[i])] = calledAddressAhead
        } else {
            for j in 0...result.count - 1 {
                if callings[i] != result[j] {
                } else if callings[i] == result[j] {
                    result[j] = result[j - 1]
                    result[j - 1] = callings[i]
                    address[callings[i]] = j - 1
                    address[result[j]] = j
                }
            }
        }
    }
    return result
}

결과

image

예상대로 초기 순위 딕셔너리를 만들어 둠으로써 연산을 확실히 줄일 수 있었다.

초기 함수의 테스트와 비교하면, 연산 속도가 드라마틱하게 줄어들었다. 두 번의 개선을 통해 그만큼 연산 횟수를 줄이는 데 성공한 것이다.

하지만 함수가 꽤 길고, 군더더기도 남아있는 듯 하다. 고차함수나 메서드를 잘 활용하면 훨씬 짧게 할 수 있을 것 같지만… 그래도 고차함수에 익숙해지기 전까지는 먼저 기초를 다지는 느낌으로 길게 나가도 나쁘지는 않을 것 같다. 당장에 코드가 긴것보다는, 쓸데없는 연산이나 코드가 없는지부터 신경써봐야겠다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.

인기 태그