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

1-H0ngJu #151

Merged
merged 4 commits into from
Mar 7, 2024
Merged

1-H0ngJu #151

merged 4 commits into from
Mar 7, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Mar 5, 2024

πŸ”— 문제 링크

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

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

1μ‹œκ°„ 30λΆ„

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

case 1은 μ²˜μŒμ— μƒκ°ν•œ 풀이이고, case 1μ—μ„œ 해결이 μ•ˆλœ 뢀뢄을 λ‹€λ₯Έ μ½”λ“œλ₯Ό μ°Ύμ•„λ³΄λ©΄μ„œ case 2둜 ν•΄κ²°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

  • case 1
    μ€‘μš”λ„κ°€ 더 높은 processκ°€ λ¨Όμ € μ²˜λ¦¬λ˜μ–΄μ•Ό ν•œλ‹€.
    λ”°λΌμ„œ μ²˜μŒμ— 제곡된 ν”„λ‘œμ„ΈμŠ€λ“€μ„ μš°μ„ μˆœμœ„μ— 따라 μ •λ ¬ν•˜λŠ” 과정이 ν•„μš”ν•˜λ‹€.

정렬은 일단 priorities[0]κ³Ό λ‹€λ₯Έ priorities의 μ›μ†Œλ₯Ό 비ꡐλ₯Ό ν•œλ‹€. λ§Œμ•½, listμ•ˆμ— μš°μ„ μˆœμœ„κ°€ 높은 processκ°€ μžˆλ‹€λ©΄ isQuededλ₯Ό True둜 μ„€μ •ν•œλ‹€.

  • isQueded(bool) : μˆœμ„œλ₯Ό λ’€λ‘œ 미뀄야 ν•˜λŠ” process (popν•˜κ³  λ‹€μ‹œ append)
def solution(priorities, location):
    answer = 0
    process = priorities[location]
    #index = location
    
    i = 0
    k = i + 1
    isQueued = False
    
    for i in range(len(priorities)):
        for k in range(len(priorities)):
            if(priorities[0] < priorities[k]):
                isQueued = True
        if(isQueued):
            tmp = priorities.pop(0)
            priorities.append(tmp)
            #print('index : ', index)
            print(priorities)
        isQueued = False
            
    return priorities

μ΄λ ‡κ²Œν•˜λ©΄ 정렬은 λ˜λ‚˜, 동일 μš°μ„ μˆœμœ„λ₯Ό 가진 process에 λŒ€ν•΄μ„œ ꡬ뢄이 νž˜λ“­λ‹ˆλ‹€.

1μ‹œκ°„ 정도 case 1둜 ν’€λ©΄μ„œ λ™μΌν•œ μš°μ„ μˆœμœ„λ₯Ό κ°€μ§€μ§€λ§Œ λ‹€λ₯Έ 인덱슀(processκ°€ 닀름)에 λŒ€ν•΄μ„œ μ–΄λ–»κ²Œ μ²˜λ¦¬ν•΄μ•Όν•˜λŠ”μ§€μ— λŒ€ν•œ μ μ ˆν•œ 풀이가 μƒκ°λ‚˜μ§€ μ•Šμ•„ λ‹€λ₯Έ λΆ„μ˜ μ½”λ“œλ₯Ό μ°Έκ³ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

  • case 2
  1. prioritiesλ₯Ό μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
  2. 큐λ₯Ό λŒλ©΄μ„œ ν”„λ‘œμ„ΈμŠ€ 수λ₯Ό μ¦κ°€μ‹œν‚¨λ‹€.
    if. 큐의 μš°μ„ μˆœμœ„μ™€ λ‚΄λ¦Όμ°¨μˆœμ˜ μš°μ„ μˆœμœ„κ°€ κ°™μœΌλ©΄, νμ—μ„œ popν•œλ‹€.
    단, location이 μΌμΉ˜ν•˜λ©΄ break
    else. μˆœμ„œλ₯Ό λ’€λ‘œ 미룬닀.

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

  • tuple
  1. μ†Œκ΄„ν˜Έλ₯Ό ν†΅ν•΄μ„œ 생성
  2. 값이 ν•˜λ‚˜λ§Œ μžˆμ–΄λ„ , ν•„μš”
  3. list와 달리, tuple의 μš”μ†ŒλŠ” μ‚­μ œν•  수 μ—†λ‹€. -> λ‹¨μˆœ κ°’ 쑰회의 경우 tuple이 효율적
  4. list보닀 λ©”λͺ¨λ¦¬κ°€ μž‘λ‹€.
  • enumerate()
    μ΅œκ·Όμ— c만 ν•˜λ‹€κ°€ μ˜€λžœλ§Œμ— python을 ν•˜λ‹ˆ 문법이 λ‚―μ„€λ‹€.
    cμ—μ„œλŠ” list의 μ›μ†Œμ— μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œλŠ” indexλ₯Ό μ¦κ°€μ‹œν‚€λ©΄μ„œ μ ‘κ·Όν•˜μ§€λ§Œ, pythonμ—μ„œλŠ” for i in list_name κ³Ό 같이 λ°”λ‘œ μ›μ†Œμ— 접근이 κ°€λŠ₯함을 잊고 μžˆμ—ˆλ‹€.

case 2의 μ½”λ“œλ₯Ό μ°Έκ³ ν•˜λ©΄μ„œ, index κ°’κ³Ό ν•΄λ‹Ή index의 μ›μ†Œμ— λ™μ‹œμ— 접근이 κ°€λŠ₯ν•œ enumerate()에 λŒ€ν•΄μ„œ μ•Œκ²Œλ˜μ—ˆλ‹€.

  • sort()
    cμ—μ„œλŠ” 정렬을 ν•˜κΈ° μœ„ν•΄μ„œλ”°λ‘œ ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜λŠ” 과정이 ν•„μš”ν–ˆλŠ”λ°, sort()λ₯Ό ν†΅ν•΄μ„œ λ‚΄λ¦Όμ°¨μˆœ, μ˜€λ¦„μ°¨μˆœ 정렬이 κ°€λŠ₯함을 μ•Œκ²Œ λ˜μ—ˆλ‹€.

  • deque
    list둜 큐λ₯Ό κ΅¬ν˜„ν•˜λŠ” κ²½μš°μ™€ 달리, dequeλ₯Ό μ‚¬μš©ν•˜λ©΄ μ‹œκ°„ λ³΅μž‘λ„κ°€ 쀄어든닀.
    list둜 κ΅¬ν˜„ν•˜λŠ” 경우(FIFO), n개의 μ›μ†Œλ₯Ό ν™•μΈν•˜λŠ” 과정이 ν•„μš”ν•˜λ―€λ‘œ, O(n)이닀.
    ν•˜μ§€λ§Œ, dequeλŠ” μ–‘λ°©ν–₯μ—μ„œ μ›μ†Œ μΆ”κ°€ 및 μ‚­μ œκ°€ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— O(1)이닀.

이 문제 전에 ν’€μ—ˆλ˜ 같은_μˆ«μžλŠ”_μ‹«μ–΄, κΈ°λŠ₯개발, μ˜¬λ°”λ₯Έ_κ΄„ν˜Έμ—μ„œλŠ” νŠΉμ • μ›μ†Œλ₯Ό popν•˜λŠ” κ²½μš°μ—λŠ” list.pop(index)둜 μ‚¬μš©ν•˜μ˜€μœΌλ‚˜, λ‹€μŒμ— ν’€κ²Œ 될 λ¬Έμ œμ—μ„œλŠ” deque둜 풀어보겠닀!

μ°Έκ³ ν•œ μ½”λ“œ

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.

ν™μ£Όλ‹˜ μ›°μ»΄ μ•Œκ³ λ¦¬λ“œλ―Έ μž…λ‹ˆλ‹€~~~~~

μ—­μ‹œ μ›λž˜ 개발 및 ν˜‘μ—…μ„ ν•˜μ‹œλ˜ 뢄이라 κ·ΈλŸ°μ§€ 넀이밍도 μ•Œμ•„λ³΄κΈ° λ„ˆλ¬΄ 쉽고 μ½”λ“œκ°€ μ§κ΄€μ μœΌλ‘œ 잘 λ³΄μ΄λ„€μš”.

μ•½κ°„ μ‚¬μ†Œν•˜μ§€λ§Œ μ€‘μš”ν•œ μ½”λ©˜νŠΈλ“€μ„ λ‚¨κ²¨λ΄€μŠ΅λ‹ˆλ‹€. κ³ μƒν•˜μ…¨μ–΄μš”!!! 😁😁😁😁

H0ngJu/README.md Outdated

| μ°¨μ‹œ | λ‚ μ§œ | λ¬Έμ œμœ ν˜• | 링크 | 풀이 |
| :---: | :--------: | :------: | :-------------------------------------------------------------------------: | :--: |
| 1μ°¨μ‹œ | 2024.03.05 | 큐 | [ν”„λ‘œμ„ΈμŠ€](https://school.programmers.co.kr/learn/courses/30/lessons/42587) | |
Copy link
Member

Choose a reason for hiding this comment

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

μ»€λ°‹μœΌλ‘œ λ¦¬λ“œλ―Έ 링크λ₯Ό ν’€μ΄λž€μ— λ„£μ–΄μ£Όμ„Έμš”!

κΏ€νŒμ„ λ“œλ¦¬μžλ©΄ λ¦¬λ“œλ―Έ 파일 μž‘μ„±ν•  λ•Œ, λ ˆν¬μ§€ν† λ¦¬ PR λ§ˆμ§€λ§‰ λ²ˆν˜Έμ—μ„œ +1ν•œ κ±°λ₯Ό 링크둜 달면 λ˜μ–΄μš”.

예λ₯Ό λ“€μ–΄ λ§ˆμ§€λ§‰ PR이 150번이면 https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/151 같이...?

그러면 PR 올릴 λ•Œ #151 μ΄λ ‡κ²Œ 뙇 λ‚˜μ˜¨λ‹΅λ‹ˆλ‹€

def solution(priorities, location):
answer = 0
queue = deque([(i, k) for k, i in enumerate(priorities)]) # νŠœν”Œ 큐 생성
priorities.sort(reverse=True) # λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬
Copy link
Member

Choose a reason for hiding this comment

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

μ €λŠ” μ§„μ§œ λ¬Έμ œλŒ€λ‘œ κ΅¬ν˜„ν•΄μ„œ ν’€μ—ˆμ—ˆλŠ”λ°,

μ–΄μ°¨ν”Ό μš°μ„ μˆœμœ„κ°€ 높은 μ• λ“€ λΆ€ν„° λ¨Όμ € λ‚˜μ™€μ•Ό ν•˜λ‹ˆ μ •λ ¬ν•΄μ„œ ν•˜λ‚˜μ”© λ½‘λŠ” 것도 아이디어 κ°„λ‹¨ν•˜κ³  μ’‹λ„€μš” πŸ‘

answer += 1 # 수 ++
if cur[1] == location: # μ°Ύκ³ μžν•˜λŠ” process인 경우
break
priorities.pop(0)
Copy link
Member

Choose a reason for hiding this comment

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

pop(0) 은 μ‹œκ°„λ³΅μž‘λ„κ°€ $O(n)$μ΄λ‹ˆκΉŒ μ–΄μ°¨ν”Ό pop(0)을 μ“°λŠ” κ±°λ©΄ priorites 도 deque둜 λž˜ν•‘ν–ˆμœΌλ©΄ 더 μ’‹μ•˜μ„ 것 κ°™μ•„μš”.

μ•„λ‹ˆλ©΄ ꡳ이 μ΄λ ‡κ²Œ ν•˜κΈ° μ‹«μœΌλ©΄

μœ„μ— μš°μ„ μˆœμœ„λ₯Ό μ •λ ¬ν•˜λŠ” μ½”λ“œλ₯Ό μ•„λž˜μ™€ 같이 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ λ°”κΎΈκ³ ,

priorities.sort() # μ˜€λ¦„μ°¨μˆœ μ •λ ¬




 while queue:
        cur = queue.popleft() # κ°€μž₯ μ•žμ˜ ν”„λ‘œμ„ΈμŠ€ κΊΌλ‚΄κΈ° (queue.pop(0)) 
        if cur[0] == priorities[-1]: # μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬λœ μš°μ„ μˆœμœ„ λ§ˆμ§€λ§‰ ν•­ == ν˜„μž¬ μˆœμœ„ -> κ°€μž₯ λ†’μŒ
            answer += 1 # 수 ++
            if cur[1] == location: # μ°Ύκ³ μžν•˜λŠ” process인 경우
                break
            priorities.pop()

둜 ν•˜λ©΄ 맀번 μš°μ„ μˆœμœ„λ₯Ό 뽑을 λ•Œ λ§ˆλ‹€ $O(1)$의 μ‹œκ°„λ³΅μž‘λ„λ‘œ λΊ„ 수 μžˆμ„ 것 κ°™μ•„μš”...!




μ§€κΈˆμ€ priorities의 크기가 100밖에 λ˜μ§€ μ•Šμ•„μ„œ μœ μ˜λ―Έν•œ 차이가 μ—†μ§€λ§Œ,

이 정도 자료 ꡬ쑰적 μ‹œκ°„ λ³΅μž‘λ„ 이득은 미리 μ²΄ν™”μ‹œν‚€λŠ” 게 쒋을 것 κ°™λ„€μš”...!




아직 자료 ꡬ쑰별 μ‹œκ°„ λ³΅μž‘λ„κ°€ 체화가 μ•ˆλ˜μ…¨μœΌλ©΄ μŠ€νƒ, 큐, νž™ 문제λ₯Ό μ§‘μ€‘μ μœΌλ‘œ ν’€μ–΄μ„œ μ²΄ν™”μ‹œμΌœλ„ 쒋을 것 κ°™μ•„μš”.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

이 문제 ν’€ λ‹Ήμ‹œμ—λŠ” popκ³Ό popleft의 차이λ₯Ό λͺ°λžλŠ”데, μ½”λ“œ λ¦¬λ·°ν•˜κ³  λ‹€μ‹œ 제 μ½”λ“œλ₯Ό λ³΄λ‹ˆκΉŒ pop보닀 popleftκ°€ 더 μ’‹μ•˜κ² λ‹€λŠ”κ²Œ λˆˆμ— λ³΄μ΄λ„€μš” γ„·γ„·

μŠ€νƒ, 큐, νž™ λ¬Έμ œλ„ μ§‘μ€‘μ μœΌλ‘œ ν’€μ–΄λ³΄κ² μŠ΄λ‹€ -!

@pknujsp
Copy link
Collaborator

pknujsp commented Mar 5, 2024

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€ λ”°λˆλ”°λˆν•œ 첫 문제 κ΅°μš”

μ €λŠ” μ΅œλŒ€ νž™(μš°μ„ μˆœμœ„ 큐)λ₯Ό μ‚¬μš©ν•΄μ„œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.

μ§€κΈˆ λ‹€μ‹œ μƒκ°ν•΄λ³΄λ‹ˆ 제 κ΅¬ν˜„μ΄ λ‹€μ†Œ λΆ€μ μ ˆν•  μˆ˜λ„ μžˆμ–΄μš”

  • μš°μ„ μˆœμœ„λ₯Ό ν•œλ²ˆ 만 정렬해놓고 priorities 큐λ₯Ό μ•žμ—μ„œ λΆ€ν„° μ°¨λ‘€λŒ€λ‘œ popν•΄κ°€λ©° μš°μ„ μˆœμœ„λ₯Ό λΉ„κ΅ν•˜λ©΄ μ΅œλŒ€ μš°μ„ μˆœμœ„μΈ ν”„λ‘œμ„ΈμŠ€λ₯Ό μ§€μšΈ λ•Œ λ¦¬μŠ€νŠΈμ—μ„œ 빼기만 ν•˜λ©΄ λ˜λ‹ˆκΉŒ O(1) 으둜 ν•œλ²ˆμ— μ²˜λ¦¬κ°€ κ°€λŠ₯ν•œλ°
  • 밑에 μ½”λ“œμ²˜λŸΌ μ΅œλŒ€ νž™μ„ μ‚¬μš©ν•΄μ„œ μš°μ„ μˆœμœ„λ₯Ό 비ꡐ해가면 μ΅œλŒ€ μš°μ„ μˆœμœ„μΈ 것을 μ§€μšΈ λ•Œ λ§ˆλ‹€ 트리λ₯Ό μ •λ¦¬ν•΄μ•Όν•΄μ„œ O(logN) 만큼 λΆˆν•„μš”ν•œ μˆ˜ν–‰ μ‹œκ°„μ΄ 더 κ±Έλ €μš”
from heapq import *
from collections import *

def solution(priorities, location):
    heap = [-p for p in priorities]
    heapify(heap)
    q = deque(enumerate(priorities))
    
    x = 1
    while q:
        i, p = q.popleft()
        p1 = -heap[0]
        
        if p1 > p:
            q.append((i, p))
            continue
        if p1 == p:
            heappop(heap)
        if i == location:
            break

        x += 1
        
    return x

@9kyo-hwang
Copy link

μ™€νμ£Όμ˜ 첫 번째 PR 🀩🀩🀩

제 κ²½μš°μ—” 덱을 μ΄μš©ν•΄ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.

from collections import deque

def solution(priorities, location):
    q = deque(enumerate(priorities))  # (인덱슀, μš°μ„ μˆœμœ„) 쌍으둜 덱에 μ‚½μž…
    answer = 1
    
    while True:
        top_loc, top_p = max(q, key=lambda x:x[1])  # κ°€μž₯ 높은 μš°μ„ μˆœμœ„ ν”„λ‘œμ„ΈμŠ€λ₯Ό 찾음
        front_loc, front_p = q.popleft()  # κ°€μž₯ μ•žμ— μžˆλŠ” ν”„λ‘œμ„Έμ„œ 팝
        
        if top_p > front_p:  # front보닀 μš°μ„ μˆœμœ„κ°€ 높은 ν”„λ‘œμ„ΈμŠ€κ°€ μ‘΄μž¬ν•œλ‹€λ©΄
            q.append((front_loc, front_p))  # 맨 뒀에 μ‚½μž…
        elif front_loc == location:  # frontκ°€ κ°€μž₯ μš°μ„ μˆœμœ„κ°€ 높은데 νƒ€κ²Ÿ location이라면
            break  # break
        else:  # μ•„λ‹ˆλΌλ©΄ 
            answer += 1  # μ‹€ν–‰ μˆœμ„œ 1 증가
            
    return answer

@Munbin-Lee
Copy link
Member

#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(vector<int> priorities, int location) {
    queue<pair<int, int>> q;
    priority_queue<int> pq;
    
    for (int i = 0; i < priorities.size(); i++) {
        q.emplace(i, priorities[i]);
        pq.emplace(priorities[i]);
    }
    
    int answer = 1;
    
    while (!q.empty()) {
        auto [index, cur] = q.front();
        
        if (cur != pq.top()) {
            q.pop();
            q.emplace(index, cur);
            continue;
        }

        if (index == location) {
            return answer;
        }
        
        q.pop();
        pq.pop();
        answer++;
    }
    
    return answer;
}

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

μ €λŠ” "μš°μ„ μˆœμœ„"λΌλŠ” 말에 ν˜„ν˜Ήλ˜μ—ˆλŠ”μ§€ 아무 생각 없이 μ΅œλŒ“κ°’μ„ μš°μ„ μˆœμœ„ 큐둜 κ΅¬ν•˜μ˜€λŠ”λ°,

λ‹€μ‹œ μƒκ°ν•΄λ³΄λ‹ˆ μ •λ ¬ν•œ ν›„ ν•˜λ‚˜μ”© pop ν•˜λŠ” 게 더 νš¨μœ¨μ μ΄κ² λ„€μš”!

ν•˜λ‚˜ 배우고 κ°‘λ‹ˆλ‹€.

@H0ngJu H0ngJu merged commit 512b3bf into main Mar 7, 2024
@H0ngJu H0ngJu deleted the 1-H0ngJu branch March 7, 2024 15:00
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