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

2-kjs254 #6

Merged
merged 3 commits into from
Feb 18, 2024
Merged

2-kjs254 #6

merged 3 commits into from
Feb 18, 2024

Conversation

kjs254
Copy link
Member

@kjs254 kjs254 commented Feb 15, 2024

πŸ”— 문제 링크

ν”„λ‘œμ„ΈμŠ€

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

1μ‹œκ°„

λ¬Έμ œμ— μ œμ‹œλœ λ‘œμ§μ„ κ΅¬ν˜„ν•˜λŠ” κ°„λ‹¨ν•œ λ¬Έμ œμ˜€μ§€λ§Œ deque을 처음 μ‚¬μš©ν•΄ 보느라 μ‹œκ°„μ΄ 많이 μž‘ν˜”μŠ΅λ‹ˆλ‹€.

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

1. 문제 μ„€λͺ…

  1. μ‹€ν–‰ λŒ€κΈ° queueμ—μ„œ λŒ€κΈ°μ€‘μΈ ν”„λ‘œμ„ΈμŠ€ ν•˜λ‚˜λ₯Ό κΊΌλƒ…λ‹ˆλ‹€.

  2. 큐에 λŒ€κΈ°μ€‘μΈ ν”„λ‘œμ„ΈμŠ€ 쀑 μš°μ„ μˆœμœ„κ°€ 더 높은 ν”„λ‘œμ„ΈμŠ€κ°€ μžˆλ‹€λ©΄ 방금 κΊΌλ‚Έ ν”„λ‘œμ„ΈμŠ€λ₯Ό λ‹€μ‹œ queue에 μ‚½μž…ν•©λ‹ˆλ‹€.

  3. λ§Œμ•½ μš°μ„ μˆœμœ„κ°€ 더 높은 ν”„λ‘œμ„ΈμŠ€κ°€ μ—†λ‹€λ©΄ 방금 κΊΌλ‚Έ ν”„λ‘œμ„ΈμŠ€λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

    • ν•œλ²ˆ μ‹€ν–‰ν•œ ν”„λ‘œμ„ΈμŠ€λŠ” λ‹€μ‹œ 큐에 넣지 μ•Šκ³  κ·ΈλŒ€λ‘œ μ’…λ£Œλ©λ‹ˆλ‹€.

ν”„λ‘œμ„ΈμŠ€μ˜ μš°μ„ μˆœμœ„κ°€ λ‹΄κΈ΄ λ°°μ—΄ priorities와 λͺ‡ λ²ˆμ§Έμ— μ‹€ν–‰λ˜λŠ”μ§€ μ•Œκ³  싢은 ν”„λ‘œμ„ΈμŠ€μ˜ μœ„μΉ˜ location이 μž…λ ₯으둜 μ£Όμ–΄μ§‘λ‹ˆλ‹€. μ—¬κΈ°μ„œ location에 μœ„μΉ˜ν•œ ν”„λ‘œμ„ΈμŠ€κ°€ λͺ‡ 번째둜 μ‹€ν–‰λ˜λŠ”μ§€ 좜λ ₯ν•˜λ©΄ λ©λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ λ°°μ—΄ [2,1,3,2]와 2κ°€ μž…λ ₯으둜 주어진닀면 λ‹€μŒκ³Ό 같이 μˆ˜ν–‰ν•©λ‹ˆλ‹€.

4개의 ν”„λ‘œμ„ΈμŠ€ [A, B, C, D]κ°€ λŒ€κΈ° 큐에 있고 μ€‘μš”λ„κ°€ [2, 1, 3, 2] μ΄λ―€λ‘œ [C, D, A, B] 순으둜 μ‹€ν–‰λ©λ‹ˆλ‹€. λ”°λΌμ„œ CλŠ” 1번째둜 μ‹€ν–‰λ©λ‹ˆλ‹€.



2. 문제 해석

μœ„ λ‘œμ§μ— 따라 μ˜ˆμ‹œλ₯Ό 돌렀보면 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

0(A) 1(B) 2(C) 3(D) μ‹€ν–‰ ν”„λ‘œμ„ΈμŠ€ (μˆœμ„œ)
1 A(2) B(1) C(3) D(2) -
2 B(1) C(3) D(2) A(2) -
3 C(3) D(2) A(2) B(1) -
4 D(2) A(2) B(1) C (1번째)
5 A(2) B(1) D (2번째)
6 B(1) A (3번째)
7 B (4번째)

location 값인 2에 ν•΄λ‹Ήν•˜λŠ” Cκ°€ 1번째둜 μ‹€ν–‰λ˜λ―€λ‘œ 1을 좜λ ₯ν•©λ‹ˆλ‹€.

μš°μ„ μˆœμœ„κ°€ 같은 값이 μ‘΄μž¬ν•˜λ―€λ‘œ λ‹¨μˆœνžˆ μš°μ„ μˆœμœ„μ— 따라 μ •λ ¬ν•˜λŠ” κ²ƒλ§ŒμœΌλ‘œλŠ” μ‹€ν–‰ μˆœμ„œλ₯Ό μ•Œ 수 μ—†μŠ΅λ‹ˆλ‹€.

μš°μ„ μˆœμœ„κ°€ κ°€μž₯ 큰 Cκ°€ 1번째둜 μ‹€ν–‰λ˜λŠ” 것은 자λͺ…ν•˜μ§€λ§Œ A와 D 쀑 λˆ„κ°€ 2번째둜 μ‹€ν–‰λ˜λŠ”μ§€ λ°”λ‘œ μ•Œ μˆ˜λŠ” μ—†κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

λ”°λΌμ„œ μ˜ˆμ‹œμ²˜λŸΌ ν”„λ‘œμ„ΈμŠ€μ˜ 이름 (ex-[A,B,C,D])을 λͺ…μ‹œν•˜κ³  λ‘œμ§μ„ 톡해 μš°μ„ μˆœμœ„ λ°°μ—΄κ³Ό 이름 배열이 λ™μ‹œμ— λŒμ•„κ°€λŠ” 방법이 κ°€μž₯ μ§κ΄€μ μž…λ‹ˆλ‹€.

ν”„λ‘œμ„ΈμŠ€μ˜ 이름을 담은 λ°°μ—΄ processesλ₯Ό μƒμ„±ν•˜μ—¬ μ½”λ“œλ₯Ό κ΅¬ν˜„ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.



3. μ½”λ“œ μž‘μ„±

  1. ν”„λ‘œμ„ΈμŠ€μ˜ 이름을 담은 λ°°μ—΄ processes 생성 ( locationκ³Ό λ§€μΉ˜ν•˜κΈ° μ‰½κ²Œ [0, 1, 2, ...]둜 μ„ μ–Έ )

    processes = list(range(len(priorities)))
  2. 맨 μ•žμ— μœ„μΉ˜ν•œ ν”„λ‘œμ„ΈμŠ€μ˜ μš°μ„ μˆœμœ„λ³΄λ‹€ 더 높은 ν”„λ‘œμ„ΈμŠ€κ°€ μ‘΄μž¬ν•˜λ©΄ priorities 맨 뒀에 pop ( processes에도 적용 )

    if priorities[0]<max(priorities): 
             priorities.append(priorities.pop(0)) 
             processes.append(processes.pop(0))
  3. 그렇지 μ•Šλ‹€λ©΄ κ·Έ ν”„λ‘œμ„ΈμŠ€λ₯Ό μ‹€ν–‰(μ‚­μ œ) ν›„ μ‹€ν–‰νšŸμˆ˜ 증가

    • μ‹€ν–‰ν•œ ν”„λ‘œμ„ΈμŠ€μ˜ 이름이 locationκ³Ό 같을 λ•Œ μ‹€ν–‰νšŸμˆ˜ λ°˜ν™˜
    else:
         del(priorities[0])
         answer +=1
             
         if processes.pop(0)==location:
             return answer

전체 μ½”λ“œ

def solution(priorities, location):
    answer = 0
    processes = list(range(len(priorities))) # ν”„λ‘œμ„ΈμŠ€ 번호 [0,1,2,...]
    
    while priorities:
        if priorities[0]<max(priorities): 
            priorities.append(priorities.pop(0)) # μš°μ„ μˆœμœ„κ°€ 더 높은 ν”„λ‘œμ„ΈμŠ€κ°€ μ‘΄μž¬ν•  λ•Œ, 큐의 맨 뒀에 μ‚½μž…
            processes.append(processes.pop(0)) # ν”„λ‘œμ„ΈμŠ€ λ²ˆν˜Έμ—λ„ λ˜‘κ°™μ΄ 적용
        else:
            del(priorities[0]) # ν”„λ‘œμ„ΈμŠ€ μ’…λ£Œ 및 μ‹€ν–‰νšŸμˆ˜ 증가
            answer +=1
            
            if location==processes.pop(0):
                return answer



4. μΆ”κ°€

μ €λ²ˆ PRμ—μ„œ λ¦¬μŠ€νŠΈμ—μ„œμ˜ μ‚½μž…μ΄λ‚˜ μ‚­μ œλŠ” λ‹€λ₯Έ μš”μ†Œλ“€μ„ μ›€μ§μ΄λŠ” μž‘μ—…μ΄ λ°œμƒν•˜μ—¬ $O(n)$만큼의 μ‹œκ°„μ΄ μ†Œμš”λœλ‹€λŠ” 점을 λ°°μ› μŠ΅λ‹ˆλ‹€.

파이썬의 collections 라이브러리의 deque을 μ΄μš©ν•˜μ—¬ μ‹œκ°„μ„ 비ꡐ μ•ˆν•΄λ³Ό 수 μ—†μ£ .

μœ„ μ½”λ“œμ˜ μžλ£Œν˜•μ„ μ „λΆ€ 큐둜 λ°”κΎΈμ–΄μ€λ‹ˆλ‹€. πŸ”₯ πŸ”₯

from collections import deque

def solution(priorities, location):
    answer = 0
    processes = deque(range(len(priorities)))
    priorities = deque(priorities)
    
    while priorities:
        if priorities[0]<max(list(priorities)):
            priorities.append(priorities.popleft())
            processes.append(processes.popleft())
        else:
            priorities.popleft()
            answer +=1
            
            if location==processes.popleft():
                return answer




비ꡐ κ²°κ³ΌλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. 😒 😒

image

μ–΄λ””κ°€ deque을 μ‚¬μš©ν•œ μΌ€μ΄μŠ€μΈμ§€ 맞좜 수 μžˆμœΌμ‹­λ‹ˆκΉŒ? μ•„λ§ˆ 아무도 λͺ»λ§žμΆœκ±°λΌ μƒκ°ν•©λ‹ˆλ‹€.

쒌츑이 listλ₯Ό μ΄μš©ν•˜μ—¬ κ±Έλ¦° μ‹œκ°„μ΄κ³  우츑이 deque을 μ΄μš©ν•˜μ—¬ κ±Έλ¦° μ‹œκ°„μž…λ‹ˆλ‹€.

μ‹œκ°„μ΄ λŠ˜μ–΄λ‚œ μΌ€μ΄μŠ€κ°€ λŒ€λ‹€μˆ˜μΈλ° λ‹€μŒκ³Ό 같은 원인이 μ˜ˆμƒλ©λ‹ˆλ‹€.
1. len() μ‚¬μš©
2. max() μ‚¬μš©
3. deque μ‚¬μš© λ―Έμˆ™

자료ꡬ쑰λ₯Ό 배울 λ•ŒλŠ” μ‰¬μ›Œλ³΄μ˜€λŠ”λ° 막상 ν™œμš©ν•˜λ €λ‹ˆ 쉽지 μ•Šμ€ 것 κ°™μŠ΅λ‹ˆλ‹€. 적극적인 ν”Όλ“œλ°± λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.

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

ν‰μ†Œμ— 라이브러리λ₯Ό 거의 μ‚¬μš©ν•˜μ§€ μ•Šκ³  문제λ₯Ό ν’€μ–΄μ™”λŠ”λ° 였늘 거의 처음으둜 μ œλŒ€λ‘œ ν™œμš©ν•΄ λ³΄μ•˜μŠ΅λ‹ˆλ‹€. μ•žμœΌλ‘œλ„ λ§Žμ€ 라이브러리λ₯Ό μ ‘ν•  μ˜ˆμ •μž…λ‹ˆλ‹€.

그리고 자료ꡬ쑰λ₯Ό μ΄μš©ν•΄ μ—°μ‚° μ‹œκ°„μ„ μ€„μ΄λŠ” λ¬Έμ œμ— λŒ€ν•œ 두렀움이 많이 μ€„μ–΄λ‘˜μ–΄μ„œ μŠ€νƒ, 큐, νž™ λ“± λ§Žμ€ 자료ꡬ쑰 λ¬Έμ œλ“€μ„ 풀어보고 μ‹Άμ–΄μ‘ŒμŠ΅λ‹ˆλ‹€.

λ§ˆμ§€λ§‰μœΌλ‘œ λ¨Έλ¦Ώμ†μœΌλ‘œ 그렀진 λ‚΄μš©μ„ μ§ˆμ„œμ •μ—°ν•˜κ²Œ μ„€λͺ…ν•˜λŠ” 것은 μ°Έ μ–΄λ €μš΄κ²ƒ κ°™μŠ΅λ‹ˆλ‹€. μ’€ 더 읽기 쉽고 νŽΈν•˜κ²Œ μž‘μ„±ν•˜λŠ” μš”λ Ήμ„ λ°°μ›Œλ‚˜κ°€μ•Όκ² μŠ΅λ‹ˆλ‹€.

deque κ΄€λ ¨ν•΄μ„œλŠ” μ•„λž˜ λΈ”λ‘œκ·Έμ˜ 도움을 λ°›μ•˜μŠ΅λ‹ˆλ‹€
좜처 : [Python] deque μ‚¬μš©λ²•

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.

βœ”οΈ μ§€μ‚°λ‹˜μ˜ 멋진 풀이와 사고과정을 λ“€μ—¬λ‹€λ³Ό 수 μžˆμ–΄μ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€.
βœ”οΈ μ§€λ‚œλ²ˆ ν”Όλ“œλ°±μ—μ„œ λ‚˜μ™”λ˜ Dequeλ₯Ό μ‚¬μš©ν•΄λ³΄μ‹  점이 특히 λ…λ³΄μ˜€κ³  ν•΄λ‹Ή κΈ°λŠ₯을 써보렀고 λ…Έλ ₯ν•˜μ…¨λ˜ 점이 μ’‹μ•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.
βœ”οΈ λ˜ν•œ Deque와 listλ₯Ό μ‚¬μš©ν•œ 방법을 λΉ„κ΅ν•΄λ΄„μœΌλ‘œμ¨ μ–΄λ–€ 것이 더 νš¨κ³Όμ μΈμ§€ λΉ„κ΅ν•΄λ³΄λŠ” 것도 μΈμƒμ μ΄μ—ˆμŠ΅λ‹ˆλ‹€.πŸ‘

βœ”οΈ μ œκ°€ 이 문제λ₯Ό λ³΄λ©΄μ„œ 제일 λ¨Όμ € λ“  생각은, μ•„ Priority_Queue λ¬Έμ œκ΅¬λ‚˜!! μ˜€μŠ΅λ‹ˆλ‹€.
♦️ λ¬Έμ œμ—μ„œ μš°μ„ μˆœμœ„λ₯Ό κ°•μ‘°ν•˜μ˜€κΈ°μ— ‼️Priority_Queue‼️λ₯Ό μ‚¬μš©ν•œλ‹€λ©΄ 더 νŽΈν•˜κ²Œ 문제λ₯Ό ν’€ 수 μžˆμ—ˆμ„ 것 κ°™μŠ΅λ‹ˆλ‹€
πŸ“ 이 방법 뿐만 μ•„λ‹ˆλΌ λ‹€μ–‘ν•œ λ°©λ²•μœΌλ‘œλ„ κ΅¬ν˜„ν•΄λ³΄μ‹œλŠ” 것을 μΆ”μ²œλ“œλ¦½λ‹ˆλ‹€.

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

@kjs254
Copy link
Member Author

kjs254 commented Feb 18, 2024

βœ”οΈ μ§€μ‚°λ‹˜μ˜ 멋진 풀이와 사고과정을 λ“€μ—¬λ‹€λ³Ό 수 μžˆμ–΄μ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€. βœ”οΈ μ§€λ‚œλ²ˆ ν”Όλ“œλ°±μ—μ„œ λ‚˜μ™”λ˜ Dequeλ₯Ό μ‚¬μš©ν•΄λ³΄μ‹  점이 특히 λ…λ³΄μ˜€κ³  ν•΄λ‹Ή κΈ°λŠ₯을 써보렀고 λ…Έλ ₯ν•˜μ…¨λ˜ 점이 μ’‹μ•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. βœ”οΈ λ˜ν•œ Deque와 listλ₯Ό μ‚¬μš©ν•œ 방법을 λΉ„κ΅ν•΄λ΄„μœΌλ‘œμ¨ μ–΄λ–€ 것이 더 νš¨κ³Όμ μΈμ§€ λΉ„κ΅ν•΄λ³΄λŠ” 것도 μΈμƒμ μ΄μ—ˆμŠ΅λ‹ˆλ‹€.πŸ‘

βœ”οΈ μ œκ°€ 이 문제λ₯Ό λ³΄λ©΄μ„œ 제일 λ¨Όμ € λ“  생각은, μ•„ Priority_Queue λ¬Έμ œκ΅¬λ‚˜!! μ˜€μŠ΅λ‹ˆλ‹€. ♦️ λ¬Έμ œμ—μ„œ μš°μ„ μˆœμœ„λ₯Ό κ°•μ‘°ν•˜μ˜€κΈ°μ— ‼️Priority_Queue‼️λ₯Ό μ‚¬μš©ν•œλ‹€λ©΄ 더 νŽΈν•˜κ²Œ 문제λ₯Ό ν’€ 수 μžˆμ—ˆμ„ 것 κ°™μŠ΅λ‹ˆλ‹€ πŸ“ 이 방법 뿐만 μ•„λ‹ˆλΌ λ‹€μ–‘ν•œ λ°©λ²•μœΌλ‘œλ„ κ΅¬ν˜„ν•΄λ³΄μ‹œλŠ” 것을 μΆ”μ²œλ“œλ¦½λ‹ˆλ‹€.

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

쒋은 ν”Όλ“œλ°± κ°μ‚¬ν•©λ‹ˆλ‹€.

μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•˜λŠ” 방법도 μžˆλ‹€λŠ” κ±Έ μ œκ°€ μ™œ λͺ°λžμ„κΉŒμš”..

queue λΌμ΄λΈŒλŸ¬λ¦¬μ— μžˆλŠ” PriorityQueueλ₯Ό μ‚¬μš©ν•˜μ—¬ 문제λ₯Ό 더 νŽΈν•˜κ²Œ ν’€ 수 μžˆμ–΄ λ³΄μž…λ‹ˆλ‹€.

ν•œ 가지 κ±Έλ¦¬λŠ” 점은 μš°μ„ μˆœμœ„κ°€ 같은 μ›μ†Œλ“€μ„ μ–΄λ–»κ²Œ μ²˜λ¦¬ν• μ§€ 금방 λ– μ˜€λ₯΄μ§€ μ•Šλ„€μš”.. ν•œλ²ˆ 도전해 보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€ ❗ ❗

@kjs254 kjs254 removed the request for review from mong3125 February 18, 2024 09:13
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.

μ˜ˆμ‹œλ₯Ό ν”„λ‘œμ„ΈμŠ€ λŒλ €μ„œ ν‘œλ‘œ 정리 ν•΄λ‘”κ±°λž‘, λ‹€λ₯Έ λ‘œμ§μ„ μ„œλ‘œ λΉ„κ΅ν•˜λ©΄μ„œ μ‹œκ°„μ„ μΈ‘μ •ν•˜μ‹ κ²Œ 인상 κΉŠμ—ˆμŠ΅λ‹ˆλ‹€. 아직 큐λ₯Ό λ°°μš°μ§€ μ•Šμ•„μ„œ μ½”λ“œ λ¦¬λ·°λŠ” μžμ„Έν•˜κ²Œ λͺ»ν•˜λŠ”κ²Œ μ•„μ‰½λ„€μš”. μ§€κΈˆ 제 λˆˆμ—λŠ” κΉ”λ”ν•˜κ³  μ™„λ²½ν•œ μ½”λ“œμ²˜λŸΌ λ³΄μž…λ‹ˆλ‹€. μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€.

@kjs254 kjs254 merged commit 17ce3a3 into AlgoLeadMe:main Feb 18, 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.

3 participants