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] 마법의 엘리베이터 #59

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

[Algorithm] 마법의 엘리베이터 #59

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

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 8, 2023

💬 문제

[코딩테스트 연습 - 마법의 엘리베이터](https://school.programmers.co.kr/learn/courses/30/lessons/148653)


💬 Idea

storey가 0이 될때까지 while문을 돌며 result를 구한다.

  1. storey를 10으로 나눈 나머지를 갖고 계산을 한다. → ex) 554: 첫 나머지는 4

  2. storey를 10으로 나눈 나머지가 6 이상일 경우, storey를 10으로 나눈 나머지가 5이면서 앞자리가 5 이상일 경우
    → 엘리베이터 상승 → 앞자리 + 1

✅ 엘리베이터가 10으로 상승하므로 result += 10 - (storey % 10)
✅ 앞자리를 올리기 위해 storey = (storey / 10) + 1
  1. storey를 10으로 나눈 나머지가 5 이하이면서 앞자리가 5보다 작을 때, storey를 10으로 나눈 나머지가 4 이하일 때
    → 엘리베이터 하강
✅ 엘리베이터가 0으로 내려가므로 result += storey % 10
✅ storey = storey / 10

💬 풀이

func solution(storey: Int) -> Int {
    var storey = storey
    var result = 0

    while storey != 0 {
        if storey % 10 > 5 || (storey % 10 == 5 && (storey / 10) % 10 >= 5) {
            result += 10 - (storey % 10)
            storey = (storey / 10) + 1
        } else {
            result += storey % 10
            storey = storey / 10
        }
    }

    return result
}
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