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

12-kjs254 #50

Merged
merged 2 commits into from
Jun 27, 2024
Merged

12-kjs254 #50

merged 2 commits into from
Jun 27, 2024

Conversation

kjs254
Copy link
Member

@kjs254 kjs254 commented Apr 11, 2024

πŸ”— 문제 링크

μ•ˆμ „μ§€λŒ€

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

1h

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

1. λ¬Έμ œν•΄μ„

image

0으둜 κ΅¬μ„±λ˜μ–΄ 있고 폭탄은 1둜 ν‘œμ‹œλ˜λŠ” n * n μ •μ‚¬κ°ν˜• 2차원 λ°°μ—΄μ—μ„œ 3*3 λ²”μœ„μ˜ ν­νƒ„μœΌλ‘œλΆ€ν„° μ•ˆμ „ν•œ μ§€λŒ€μ˜ 개수λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

ꡉμž₯히 κ°„λ‹¨ν•΄μ„œ λ‹€μ–‘ν•œ 풀이방법이 λ²ˆλœ©μ΄λŠ” λ¬Έμ œλ„€μš”.

μ œκ°€ λ– μ˜¬λ¦° 방법은 λ‹€μŒμ΄ μžˆμŠ΅λ‹ˆλ‹€.

  1. ν­νƒ„μ˜ κ°œμˆ˜μ™€ μœ„μΉ˜λ§Œμ„ νŒŒμ•…ν•΄μ„œ n2μ—μ„œ ν­νƒ„μ˜ μœ„ν—˜μ§€μ—­μ˜ 개수λ₯Ό λΉΌμ£ΌλŠ” 방법

  2. 배열을 ν•˜λ‚˜μ”© μˆœνšŒν•˜μ—¬ 폭탄이 λ°œκ²¬λœλ‹€λ©΄ μƒν•˜ 쒌우 λŒ€κ°μ„ μ— -1을 μ‚½μž…ν•˜μ—¬ 0의 개수λ₯Ό μ„ΈλŠ” 방법

  3. 배열을 μˆœνšŒν•˜μ—¬ λ‚˜μ˜ μƒν•˜ 쒌우 λŒ€κ°μ„ μ— 폭탄이 μžˆλŠ”μ§€ μ²΄ν‚Ήν•˜λ©΄μ„œ μ§„ν–‰ν•˜λŠ” 방법

μˆ˜ν•™μ μœΌλ‘œ μ ‘κ·Όν•˜λŠ” 1번의 연산속도가 λΉ λ₯΄μ§€λ§Œ λ”°μ Έμ•Όν•˜λŠ” 쑰건이 μƒλ‹Ήν•œ 터라 2번의 방법을 μ„ νƒν–ˆμŠ΅λ‹ˆλ‹€.

3λ²ˆμ€ 2번과 μœ μ‚¬ν•œ λ°©λ²•μ΄μ§€λ§Œ 괜히 μ‹œκ°„λ³΅μž‘λ„λ§Œ λŠ˜λ¦¬λŠ” 방법이라 μ—¬κΈ°μ„œ μ“Έ 방법은 μ•„λ‹Œκ²ƒ κ°™λ„€μš”.

2차원 배열을 μˆœνšŒν•˜λ©° ν­νƒ„μ˜ μƒν•˜μ’Œμš°λŒ€κ°μ„ μ„ 닀져보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.



2. μˆ˜λ„μ½”λ“œ

  1. ν­νƒ„μ˜ μƒν•˜μ’Œμš°λ₯Ό -1둜 μΉ˜ν™˜ν•œλ‹€.

  2. boradμ—μ„œ 0의 개수λ₯Ό μ„Όλ‹€.

μ—¬κΈ°μ„œ 쑰건이 λͺ‡ 개 λΆ™μŠ΅λ‹ˆλ‹€.

board[i][j] 둜 배열에 접근을 ν•˜λ €λ‹ˆ ν­νƒ„μ˜ μœ„μΉ˜μ— 따라 λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λŠ” λ¬Έμ œκ°€ μžˆμ–΄μ„œ 쑰건을 뢙이기보닀 κ·Έλƒ₯ ν…Œλ‘λ¦¬λ₯Ό κ°μ‹Έμ£ΌλŠ”κ²Œ νŽΈν•΄λ³΄μ—¬μ„œ insert(), append() λ™μ›ν•΄μ„œ 배열을 κ°μ‹Έμ€¬μŠ΅λ‹ˆλ‹€.

n = len(board)
    for i in range(n):
        board[i].insert(0,-1)
        board[i].append(-1)
    board.insert(0,[-1]*(n+2))
    board.append([-1]*(n+2))

ν…Œλ‘λ¦¬μ™€ μœ„ν—˜μ§€μ—­μ— -1을 μ‚½μž…ν•œλ‹€λ©΄ 5*5 배열은 λ‹€μŒκ³Ό 같은 λͺ¨μŠ΅μ΄ λ˜μ–΄μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.

image

이제 인덱싱 λ²”μœ„λ₯Ό λ²—μ–΄λ‚  걱정이 μ—†μœΌλ‹ˆ νƒμƒ‰ν•˜μ—¬ μœ„ν—˜μ§€μ—­μ„ -1둜 μΉ˜ν™˜ν•΄μ£Όκ³  0의 개수λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

for i in range(n+2):
        for j in range(n+2):
            
            if board[i][j]==1:
                for dx in (-1,0,1):
                    for dy in (-1,0,1):
                        board[i+dy][j+dx] =-1
    
return sum(board, []).count(0) #1차원 λ°°μ—΄λ‘œ λ°”κΎΌ ν›„ 카운트

ν•˜μ§€λ§Œ μ΄λ ‡κ²Œ ν’€λ©΄ μ˜€λ‹΅μž…λ‹ˆλ‹€.

폭탄이 μΈμ ‘ν•΄μžˆλ‹€λ©΄ 1이 -1둜 μΉ˜ν™˜λ˜μ–΄ 폭탄을 μˆ¨κ²¨λ²„λ¦½λ‹ˆλ‹€.

쑰건을 달면 λ°”λ‘œ ν•΄κ²°λ˜κ² μ§€λ§Œ λ°°μ—΄μ˜ 값이 μ •μˆ˜λΌλŠ” 점을 μ΄μš©ν•˜μ—¬ μ—°μ‚°λ§ŒμœΌλ‘œ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

board[i+dy][j+dx] = board[i+dy][j+dx]*2 -1

ν•¨μˆ˜ f(x) = 2x-1 μ—μ„œ f(1) = 1, f(0) = -1μ΄λ―€λ‘œ 값이 1인 폭탄이 λˆ„λ½λ  일이 μ—†μ–΄μ‘ŒμŠ΅λ‹ˆλ‹€.

ν˜Ήμ‹œ 0 값이 μΆ”κ°€λ˜μ–΄ 정닡에 영ν–₯을 μ€„κΉŒ μ‹Άμ§€λ§Œ f(0.5) = 0이기 λ•Œλ¬Έμ— λ¬Έμ œμ—†μŠ΅λ‹ˆλ‹€.

전체 μ½”λ“œ

def solution(board):
    n = len(board)
    for i in range(n):
        board[i].insert(0,-1)
        board[i].append(-1)
    board.insert(0,[-1]*(n+2))
    board.append([-1]*(n+2))
    
    for i in range(n+2):
        for j in range(n+2):
            
            if board[i][j]==1:
                for dx in (-1,0,1):
                    for dy in (-1,0,1):
                        board[i+dy][j+dx] = board[i+dy][j+dx]*2 -1
    
    return sum(board, []).count(0)

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

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ μ½”λ”©ν…ŒμŠ€νŠΈ μž…λ¬Έλ¬Έμ œλΌ κ·Έμ € 재밌게 ν’€κΈ°λ§Œ ν•΄μ„œ μƒˆλ‘­κ²Œ μ•Œκ²Œ 된 것은 μ—†λ„€μš”

λ‹€μŒμ—” μœ μ‚¬ν•˜μ§€λ§Œ λΉ„μŠ·ν•œ 문제둜 가지고 μ™€λ³΄κ² μŠ΅λ‹ˆλ‹€!

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

νŒ¨λ”©μ‹œμΌœμ„œ ν‘ΈλŠ” 방법 μƒˆλ‘­λ„€μš” :) μ „ 일반적인 방식인 out_of_bound 체크 및 λ°©λ¬Έ ν‘œμ‹œ λ°©λ²•μœΌλ‘œ ν’€μ—ˆμ—ˆλŠ”λ°...

Comment on lines +3 to +7
for i in range(n):
board[i].insert(0,-1)
board[i].append(-1)
board.insert(0,[-1]*(n+2))
board.append([-1]*(n+2))

Choose a reason for hiding this comment

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

n = len(board)
board = [[-1, *line, -1] for line in board]  # 각 ν–‰λ§ˆλ‹€ 맨 μ•ž, 맨 뒀에 -1 λΆ™μž„
board = [[-1] * (n + 2), *board, [-1] * (n + 2)]  # 0ν–‰ μ•ž, λ§ˆμ§€λ§‰ ν–‰ 뒀에 [-1, ...] 1차원 리슀트 λΆ™μž„

-1 νŒ¨λ”©μ€ μš”λ ‡κ²Œ 2μ€„λ‘œ ν•  수 μžˆλ‹΅λ‹ˆλ‹€ :)

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

νŒ¨λ”©μ‹œμΌœμ„œ ν‘ΈλŠ” 방법이 되게 μΈμƒκΉŠμ—ˆλ˜ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€. μƒˆλ‘­κ²Œ 방법 ν•˜λ‚˜λ₯Ό λ°°μ›Œκ°ˆ 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
μˆ˜ν•™μ  연산을 ν†΅ν•˜μ—¬ ν­νƒ„μ˜ μœ„μΉ˜λ₯Ό 쑰건없이 μ°ΎλŠ” 방법도 μ‹ κΈ°ν–ˆμŠ΅λ‹ˆλ‹€.
μ˜ˆμ „μ— λΉ„μŠ·ν•œ 문제λ₯Ό ν’€λ˜ 쀑 μž…λ ₯을 받을 λ•Œ ν­νƒ„μ˜ μœ„μΉ˜λ₯Ό 미리 기얡을 ν•΄λ‘” λ’€ ν•΄λ‹Ή μœ„μΉ˜μ— λŒ€ν•˜μ—¬ 연산을 μˆ˜ν–‰ν•¨μœΌλ‘œμ„œ μ†Œμš”μ‹œκ°„μ„ 쀄일 수 μžˆλŠ” 방법이 μžˆμ—ˆλ˜ κ²ƒμœΌλ‘œ κΈ°μ–΅ν•˜λŠ”λ° 이 λ¬Έμ œμ—μ„œλ„ μ μš©κ°€λŠ₯ν•  것 κ°™λ‹€λŠ” 생각을 ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€. 😁

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

νŒ¨λ”©μ‹œμΌœμ„œ ν‘ΈλŠ” 것도 인상 κΉŠμ—ˆκ³ , μ €λŠ” μ •μˆ˜μ˜ νŠΉμ„±μ„ μ΄μš©ν•΄μ„œ 쑰건문 μ•ˆ 뢙이고 *2 -1 λΆ™μ—¬μ„œ ν•˜λŠ”κ±΄ μ§„μ§œ λ†€λžλ„€μš”.. 이건 저도 μ•žμœΌλ‘œ 자주 μ¨λ¨Ήμ–΄μ•Όκ² μŠ΅λ‹ˆλ‹€. μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€.

@tgyuuAn tgyuuAn merged commit 552dbe0 into AlgoLeadMe:main Jun 27, 2024
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.

5 participants