Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] [3차] n진수 게임 #99

Closed
hwangJi-dev opened this issue Jan 28, 2023 · 0 comments
Closed

[Algorithm] [3차] n진수 게임 #99

hwangJi-dev opened this issue Jan 28, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 28, 2023

💬 문제

[코딩테스트 연습 - [3차] n진수 게임](https://school.programmers.co.kr/learn/courses/30/lessons/17687)


💬 Idea

  • 미리 구할 숫자의 갯수 t, 게임에 참가하는 인원 m 을 곱한만큼 for문을 돌면서 해당 게임에서 나오는 N진수를 game 배열에 더해준다.
    • 이 때 10 이상의 숫자부터는 한자리씩 끊어서 말하므로 N진수를 Array로 변환하여 저장해준다.
      • ex) 10 → ”1”, “0” 으로 append
  • 튜브의 순서인 p에서부터 (인덱스 계산을 위해 p - 1) t * m까지를 돌며 게임 참가 인원인 m만큼 스텝핑하여 얻은 결과를 정답 String에 더해준다.

💬 풀이

func solution(_ n:Int, _ t:Int, _ m:Int, _ p:Int) -> String {
    var ans = ""
    var game: [String] = []
    
    for i in 0..<(t * m) {
        game.append(contentsOf: Array(String(i, radix: n).uppercased()).map({ String($0) }))
    }
    
    for g in stride(from: p - 1, to: t * m, by: m) {
        ans += game[g]
    }
    
    return ans
}

💬 더 나은 방법?

func solution(_ n:Int, _ t:Int, _ m:Int, _ p:Int) -> String {
    var game: [String] = []
    
    for i in 0..<(t * m) {
        game.append(contentsOf: Array(String(i, radix: n).uppercased()).map({ String($0) }))
    }
    
    return stride(from: p - 1, to: t * m, by: m).reduce("", { $0 + game[$1] })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant