-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
There was a problem hiding this 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) | | |
There was a problem hiding this comment.
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) # λ΄λ¦Όμ°¨μ μ λ ¬ |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()
λ‘ νλ©΄ λ§€λ² μ°μ μμλ₯Ό λ½μ λ λ§λ€
μ§κΈμ priorities
μ ν¬κΈ°κ° 100λ°μ λμ§ μμμ μ μλ―Έν μ°¨μ΄κ° μμ§λ§,
μ΄ μ λ μλ£ κ΅¬μ‘°μ μκ° λ³΅μ‘λ μ΄λμ 미리 체νμν€λ κ² μ’μ κ² κ°λ€μ...!
μμ§ μλ£ κ΅¬μ‘°λ³ μκ° λ³΅μ‘λκ° μ²΄νκ° μλμ ¨μΌλ©΄ μ€ν, ν, ν λ¬Έμ λ₯Ό μ§μ€μ μΌλ‘ νμ΄μ 체νμμΌλ μ’μ κ² κ°μμ.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄ λ¬Έμ ν λΉμμλ popκ³Ό popleftμ μ°¨μ΄λ₯Ό λͺ°λλλ°, μ½λ 리뷰νκ³ λ€μ μ μ½λλ₯Ό 보λκΉ popλ³΄λ€ popleftκ° λ μ’μκ² λ€λκ² λμ 보μ΄λ€μ γ·γ·
μ€ν, ν, ν λ¬Έμ λ μ§μ€μ μΌλ‘ νμ΄λ³΄κ² μ΄λ€ -!
μκ³ νμ ¨μ΅λλ€ λ°λλ°λν 첫 λ¬Έμ κ΅°μ μ λ μ΅λ ν(μ°μ μμ ν)λ₯Ό μ¬μ©ν΄μ νμ΄λ΄€μ΅λλ€. μ§κΈ λ€μ μκ°ν΄λ³΄λ μ ꡬνμ΄ λ€μ λΆμ μ ν μλ μμ΄μ
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 |
μνμ£Όμ 첫 λ²μ§Έ 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 |
#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;
} κ³ μνμ ¨μ΅λλ€. μ λ "μ°μ μμ"λΌλ λ§μ ννΉλμλμ§ μ무 μκ° μμ΄ μ΅λκ°μ μ°μ μμ νλ‘ κ΅¬νμλλ°, λ€μ μκ°ν΄λ³΄λ μ λ ¬ν ν νλμ© νλ λ°°μ°κ³ κ°λλ€. |
π λ¬Έμ λ§ν¬
νλ‘μΈμ€
βοΈ μμλ μκ°
1μκ° 30λΆ
β¨ μλ μ½λ
case 1μ μ²μμ μκ°ν νμ΄μ΄κ³ , case 1μμ ν΄κ²°μ΄ μλ λΆλΆμ λ€λ₯Έ μ½λλ₯Ό μ°Ύμ보면μ case 2λ‘ ν΄κ²°νμμ΅λλ€.
μ€μλκ° λ λμ processκ° λ¨Όμ μ²λ¦¬λμ΄μΌ νλ€.
λ°λΌμ μ²μμ μ 곡λ νλ‘μΈμ€λ€μ μ°μ μμμ λ°λΌ μ λ ¬νλ κ³Όμ μ΄ νμνλ€.
μ λ ¬μ μΌλ¨ priorities[0]κ³Ό λ€λ₯Έ prioritiesμ μμλ₯Ό λΉκ΅λ₯Ό νλ€. λ§μ½, listμμ μ°μ μμκ° λμ processκ° μλ€λ©΄ isQuededλ₯Ό Trueλ‘ μ€μ νλ€.
μ΄λ κ²νλ©΄ μ λ ¬μ λλ, λμΌ μ°μ μμλ₯Ό κ°μ§ processμ λν΄μ ꡬλΆμ΄ νλλλ€.
1μκ° μ λ case 1λ‘ νλ©΄μ λμΌν μ°μ μμλ₯Ό κ°μ§μ§λ§ λ€λ₯Έ μΈλ±μ€(processκ° λ€λ¦)μ λν΄μ μ΄λ»κ² μ²λ¦¬ν΄μΌνλμ§μ λν μ μ ν νμ΄κ° μκ°λμ§ μμ λ€λ₯Έ λΆμ μ½λλ₯Ό μ°Έκ³ νμμ΅λλ€.
if. νμ μ°μ μμμ λ΄λ¦Όμ°¨μμ μ°μ μμκ° κ°μΌλ©΄, νμμ popνλ€.
λ¨, locationμ΄ μΌμΉνλ©΄ break
else. μμλ₯Ό λ€λ‘ 미룬λ€.
π μλ‘κ² μκ²λ λ΄μ©
μ΅κ·Όμ 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λ‘ νμ΄λ³΄κ² λ€!
μ°Έκ³ ν μ½λ