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

59-9kyo-hwang #210

Merged
merged 10 commits into from
Sep 19, 2024
Merged

59-9kyo-hwang #210

merged 10 commits into from
Sep 19, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

πŸ”— 문제 링크

λ§ˆλ²•μ˜ μ—˜λ¦¬λ² μ΄ν„°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

1. μ„€λͺ…

ν˜„μž¬ λ‚˜μ™€ μ—˜λ¦¬λ² μ΄ν„°λŠ” storey($1 \le storey \le 100,000,000$) 측에 μžˆλ‹€. $-1, +1, -10, +10, -100, +100, ...$κ³Ό 같이 μ ˆλŒ€κ°’μ΄ $10^c$($c \ge 0$) ν˜•νƒœμΈ μ •μˆ˜λ“€μ΄ 적힌 λ²„νŠΌ μ—˜λ¦¬λ² μ΄ν„°λ₯Ό 톡해 0측으둜 κ°€κ³ μž ν•œλ‹€.
ν•΄λ‹Ή λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ ν˜„μž¬ μΈ΅ μˆ˜μ— λ²„νŠΌμ— μ ν˜€ μžˆλŠ” 값을 λ”ν•œ 측으둜 μ΄λ™ν•˜κ²Œ λ˜λŠ”λ°, μ—˜λ¦¬λ² μ΄ν„°κ°€ μœ„μΉ˜ν•΄ μžˆλŠ” μΈ΅κ³Ό λ²„νŠΌμ˜ 값을 λ”ν•œ κ²°κ³Όκ°€ 0보닀 μž‘μœΌλ©΄ μ—˜λ¦¬λ² μ΄ν„°λŠ” 움직이지 μ•ŠλŠ”λ‹€.

μ—˜λ¦¬λ² μ΄ν„°λ₯Ό 움직이기 μœ„ν•΄μ„œ λ²„νŠΌ ν•œ λ²ˆλ‹Ή λ§ˆλ²•μ˜ 돌 ν•œ 개λ₯Ό μ‚¬μš©ν•œλ‹€.

  • 예λ₯Ό λ“€μ–΄, 16μΈ΅μ—μ„œ 0측으둜 κ°€λ €λ©΄ -1이 적힌 λ²„νŠΌμ„ 6번, -10이 적힌 λ²„νŠΌμ„ 1번 눌러 λ§ˆλ²•μ˜ 돌 7개λ₯Ό μ†Œλͺ¨ν•˜μ—¬ 0측으둜 갈 수 μžˆλ‹€.
  • ν•˜μ§€λ§Œ, +1이 적힌 λ²„νŠΌμ„ 4번, -10이 적힌 λ²„νŠΌ 2λ²ˆμ„ λˆ„λ₯΄λ©΄ λ§ˆλ²•μ˜ 돌 6개λ₯Ό μ†Œλͺ¨ν•˜μ—¬ 0측으둜 갈 수 μžˆλ‹€.

이와 같이 μ–΄λ–€ μΈ΅μ—μ„œ μ—˜λ¦¬λ² μ΄ν„°λ₯Ό 타고 0측으둜 λ‚΄λ €κ°€λŠ”λ° ν•„μš”ν•œ λ§ˆλ²•μ˜ 돌의 μ΅œμ†Œ 개수λ₯Ό μ•Œκ³ μž ν•œλ‹€.
ν˜„μž¬ 측을 λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜ storeyκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, 0측으둜 κ°€κΈ° μœ„ν•΄ ν•„μš”ν•œ λ§ˆλ²•μ˜ 돌의 μ΅œμ†Œκ°’μ„ return ν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•˜λΌ.

2. 풀이

μ²˜μŒμ— 문제λ₯Ό λ”± 보자마자 1둜 λ§Œλ“€κΈ° λ¬Έμ œλ‚˜ μˆ¨λ°”κΌ­μ§ˆ 거의 λΉ„μŠ·ν•΄ λ³΄μ—¬μ„œ 거리 ν…Œμ΄λΈ”κ³Ό queueλ₯Ό μ΄μš©ν•œ BFS둜 ν’€λ €κ³  ν–ˆμœΌλ‚˜...

BFS μ‹œλ„ μ½”λ“œ

#include <vector>
#include <queue>
#include <unordered_map>
#include <cmath>

using namespace std;

int solution(int Storey) 
{
    vector<int> Buttons;
    int Button;
    for(int c = 0; (Button = pow(10, c)) < Storey; ++c)
    {
        Buttons.emplace_back(Button);
        Buttons.emplace_back(-1 * Button);
    }
    
    deque<int> Q;
    unordered_map<int, int> Consumptions;
    
    Q.emplace_back(Storey);
    Consumptions[Storey] = 0;
    
    while(!Q.empty())
    {
        const int Floor = Q.front();
        Q.pop_front();
        
        if(Floor == 0) break;

        for(const int Button : Buttons)
        {
            int NextFloor = Floor + Button;
            if(NextFloor >= 0 && Consumptions.count(NextFloor) == 0)
            {
                Consumptions[NextFloor] = Consumptions[Floor] + 1;
                Q.emplace_back(NextFloor);
            }
        }
    }
    
    return Consumptions[0];
}

거의 λͺ¨λ“  ν…ŒμΌ€μ—μ„œ μ‹œκ°„ μ΄ˆκ³Όκ°€ λ°œμƒν–ˆλ‹€.
1둜 λ§Œλ“€κΈ°λŠ” 무쑰건 μˆ«μžκ°€ κ°μ†Œν•˜λŠ” 것밖에 μ—†κΈ° λ•Œλ¬Έμ— μΌ€μ΄μŠ€λ₯Ό κ·€κ²°μ‹œν‚¬ 수 μžˆμ—ˆκ³ , μˆ¨λ°”κΌ­μ§ˆμ€ 숫자 λ²”μœ„κ°€ 쒁기 λ•Œλ¬Έμ— λ²—μ–΄λ‚˜λŠ” μΌ€μ΄μŠ€λ₯Ό 많이 κ±°λ₯Ό 수 μžˆμ—ˆμ§€λ§Œ 이 λ¬Έμ œλŠ” μ•„λ‹ˆμ—ˆλ‹€...
κ²°κ΅­ μ‚½μ§ˆ μ’€ ν•˜λ‹€κ°€ λ‹€ μ§€μš°κ³  질문 κ²Œμ‹œνŒμ„ 확인해보고 λ‚˜μ„œ 감을 μž‘μ•˜λ‹€.

핡심은 "끝자리 숫자λ₯Ό ν™•μΈν•΄μ„œ λ”ν•˜κΈ°/λΉΌκΈ° μˆ˜ν–‰ν•˜κΈ°"이닀.

  1. 예λ₯Ό λ“€μ–΄ 16의 경우 μ•„λž˜μ™€ 같이 μˆ˜ν–‰ν•΄μ„œ 6번이 μ΅œμ†Œμ΄λ‹€.
  • 16 -> 20(+1 4번)
  • 20 -> 0(-10 2번): 20μ—μ„œ -10 * 2번 == 2μ—μ„œ -1 * 2번.
  1. 예λ₯Ό λ“€μ–΄ 2554의 경우 μ•„λž˜μ™€ 같이 μˆ˜ν–‰ν•΄μ„œ 16번이 μ΅œμ†Œμ΄λ‹€.
  • 2554 -> 2550(-1 4번)
  • 2550 -> 2600(+10 5번): 2550μ—μ„œ +10 * 5번 == 255μ—μ„œ +1 * 5번
  • 2600 -> 3000(+100 4번): 2600μ—μ„œ +100 * 4번 == 26μ—μ„œ +1 * 4번
  • 3000 -> 0(-1000 4번): 3000μ—μ„œ -1000 4번 == 3μ—μ„œ -1 * 4번

μ˜ˆμ‹œλ₯Ό 보면 μ•Œ 수 μžˆλ“―, 뒀에 0이 λΆ™μ–΄μžˆλŠ” κ²½μš°λŠ” 사싀상 λ–Όκ³  생각해도 λ¬΄λ°©ν•˜λ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€.
μ£Όμ˜ν•΄μ„œ 봐야할 점은 μˆ«μžκ°€ "5"인 경우인데

  • 0 ~ 4λŠ” -1을 μˆ˜ν–‰ν•˜λ©΄ λœλ‹€.
  • 6 ~ 9λŠ” +1을 μˆ˜ν–‰ν•˜λ©΄ λœλ‹€.
  • 5의 경우 "λ‹€μŒ μžλ¦¬κ°€ 5 이상이면 +1, μ•„λ‹ˆλΌλ©΄ -1"을 μˆ˜ν–‰ν•œλ‹€.

예λ₯Ό λ“€μ–΄, 65λ₯Ό 0으둜 λ§Œλ“ λ‹€κ³  ν•˜λ©΄

  • 65 -> 70(+1 5번) -> 100(+10 3번) -> 0(-100 1번): 9번
  • 65 -> 60(-1 5번) -> 100(+10 4번) -> 0(-100 1번): 10번

μ΄λ ‡κ²Œ 5μ—μ„œ +1/-1 ν•˜λŠ” 거에 따라 νšŸμˆ˜κ°€ λ‹¬λΌμ§€λŠ”λ°, +1 ν•˜λŠ” 것이 더 νšŸμˆ˜κ°€ μ μŒμ„ μ•Œ 수 μžˆλ‹€. μ΄λŠ” μ•ž μžλ¦¬κ°€ "6"인 것을 보고 +1을 ν–ˆκΈ° λ•Œλ¬Έμ΄λ‹€.

이 κ·œμΉ™μ„ μ£Όμ˜ν•΄μ„œ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ λœλ‹€.

int solution(int Storey) 
{
    int Answer = 0;
    
    while(Storey)
    {
        int Remainder = Storey % 10;
        int Quotient = Storey / 10;
        
        if(Remainder < 5)  // 끝자리 μˆ«μžκ°€ 0 ~ 4
        {
            Answer += Remainder;  // 숫자만큼 -1
        }
        else if(Remainder > 5)  // 끝자리 μˆ«μžκ°€ 6 ~ 9
        {
            ++Quotient;  // 자릿수 올림이 λ°œμƒν•¨
            Answer += 10 - Remainder;  // 숫자만큼 +1
        }
        else  // 끝자리 μˆ«μžκ°€ 5
        {
            if(Quotient % 10 >= 5)  // λ‹€μŒ 자리 μˆ«μžκ°€ 5 이상이면
            {
                ++Quotient;  // +1을 ν•΄μ•Όν•˜λ―€λ‘œ 자릿수 올림
            }
            
            Answer += Remainder;  // 숫자만큼 +1/-1
        }
        
        Storey = Quotient;  // κ°±μ‹ λœ λͺ«μœΌλ‘œ κ°±μ‹ 
    }
    
    return Answer;
}

μ½”λ“œλŠ” κ°„λ‹¨ν•˜λ‹€. μœ„μ—μ„œ μ–˜κΈ°ν•œ μΌ€μ΄μŠ€λ₯Ό κ·ΈλŒ€λ‘œ κ΅¬ν˜„ν•œ 것이닀.
끝 자릿수λ₯Ό κ·Έλ•Œκ·Έλ•Œ ν™•μΈν•΄μ„œ 5 초과 / 5 미만 / 5 3가지 경우둜 λ‚˜λˆ  λͺ«μ„ λ³΄μ •ν•œ λ’€ λ²„νŠΌ λˆ„λ₯Έ 횟수λ₯Ό 더해주면 λœλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μ•½κ°„ μ‘°μ΄μŠ€ν‹± λ¬Έμ œλž‘ λΉ„μŠ·ν•œ 것 같기도 ν•˜λ‹€...

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def solution(storey):
    
    answer = 0
    flag = False
    storey = str(storey)
    for y in range(len(storey)-1,-1,-1):
        x = int(str(storey)[y])
        
        if(flag == True):
            x += 1
            flag = False
            
        if(y==0):
            if x>=6: answer += 10-x+1
            else: answer += x
            return answer

        if(x>=6):
            answer += (10 - x)
            flag = True
            
        elif(x<=4): answer += x
        
        else:
            if y>=1:
                if int(storey[y-1])>=5:
                    answer += (10 - x)
                    flag = True
                else: answer += x

μ‹«μ–΄ν•˜λŠ” μœ ν˜•μ˜ 문제..

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5일 λ•Œ, 5μ¦κ°€μ‹œν‚¬μ§€ 5λ₯Ό 빼버릴지 경계선 μ°ΎλŠ”λ° ν—€λ§Έλ„€μš” γ…‹γ…‹γ…‹ γ… 
μ‹œν—˜μž₯μ—μ„œ μ‹€μ œλ‘œ λ§Œλ‚˜λ©΄ 등골 μ˜€μ‹Ήν•  λ“―ν•©λ‹ˆλ‹€..γ…Ž

def solution(storey):
    answer = 0
    carry = False 
    numbers = list(str(storey))[::-1]  
    n = len(numbers)
    
    for i in range(len(numbers)):
        now = int(numbers[i]) + carry
        answer += now
        carry = False 
        
        if now > 5:
            answer -= (-10 + now*2)
            carry = True
            continue 
            
        if now == 5 and i != n-1:
            next = int(numbers[i+1])
            if next >= 5 :
                carry = True 
            continue 
            
    if carry:
        answer += 1
    
    return answer

@9kyo-hwang 9kyo-hwang merged commit 91fa21c into main Sep 19, 2024
4 checks passed
@9kyo-hwang 9kyo-hwang deleted the 59-9kyo-hwang branch September 19, 2024 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants