[Level 3] N으로 표현 + Swift

sanghee·2021년 9월 7일
0

🙈코딩테스트

목록 보기
23/52
post-thumbnail

[Level 3] N으로 표현

https://programmers.co.kr/learn/courses/30/lessons/42895

풀이

func solution(_ N: Int, _ number: Int) -> Int {
    var result = -1
    
    func getAnswer(count: Int, now: Int) {
        if count > 8 { return }
        if (result != -1) && (count >= result) { return }

        if now == number {
            result = count
            return
        }
        
        var newNum = 0
        for i in 1..<(9-count) {
            newNum = newNum * 10 + N
            getAnswer(count: count + i, now: now + newNum)
            getAnswer(count: count + i, now: now - newNum)
            getAnswer(count: count + i, now: now * newNum)
            getAnswer(count: count + i, now: now / newNum)
        }
    }
    
    getAnswer(count: 0, now: 0)
    
    return result
}

solution(N, number)
profile
👩‍💻

0개의 댓글