-
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
39-pknujsp #163
39-pknujsp #163
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.
from heapq import *
from collections import deque
def solution(jobs):
n = len(jobs)
jobs.sort()
jobs = deque(jobs)
heap = []
answer = 0
now = -1
while jobs:
input_time, duration_time = jobs.popleft()
print()
print(answer)
print(now)
print(heap)
print(input_time, duration_time)
if input_time == now:
keep_job = [now+duration_time, duration_time, input_time]
heappush(heap, keep_job)
elif input_time > now:
# μννκ³ μμ§ μμ κ²½μ° λ¨Όμ λ€μ΄μ¨ κ²μ μν
if len(heap) == 0:
now = input_time + duration_time
answer += duration_time
continue
# νμ¬ μκ°μ κΈ°μ€μΌλ‘ κ°±μ
while heap[0][0] != now + heap[0][1]:
temp = heappop(heap)
heappush(heap, [now + temp[1], temp[1], temp[2]])
peek = heappop(heap)
now = peek[0]
answer += peek[0] - peek[2]
keep_job = [now+duration_time, duration_time, input_time]
heappush(heap, keep_job)
# μννκ³ μλ μμ
μ΄ μμ κ²½μ°,
else:
# μ§κΈ μ§νμ€μΈ μμ
μ΄ λλμλ§μ λ£μμ κ²½μ° λλλ μκ°, μμ², 걸리λ μκ°
keep_job = [now+duration_time, duration_time, input_time]
heappush(heap, keep_job)
while heap:
print()
print(answer)
print(now)
print(heap)
while heap[0][0] != now + heap[0][1]:
temp = heappop(heap)
heappush(heap, [now + temp[1], temp[1], temp[2]])
peek = heappop(heap)
if peek[1] + peek[2] >= peek[0]:
now = peek[1]+ peek[2]
answer += peek[1]
else:
now = peek[0]
answer += peek[0] - peek[2]
print(now)
print(answer)
return answer//n
# λ°μ΄ν° λ€μ΄μ€λ©΄ νμΌλ‘ μ§μ΄λ£μ.
# κΈ°λ€λ¦¬λ μκ° + μ§νλλ μκ°μ΄ κ°μ₯ μ μ κ² λΆν° μ€νμν΄.
μ΄ μ½λκ° κ³μ 9λ² ν μ€νΈ μΌμ΄μ€μ κ±Έλ €μ..
1μκ° μ§ΈμΈλ° μ λͺ¨λ₯΄κ² λ€μ..
jobs = [[0, 6], [2, 8], [3, 7], [7, 1], [11, 11], [19, 25], [30, 15], [32, 6], [40, 3]]
answer : 19
κ° λμμΌ νλλ° μκΎΈ 18μ΄ λμ€λ€μ... μμ£ γ γ ... ν..
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.
import heapq
def solution(jobs):
jobs.sort()
current_time = 0
total_time = 0
heap = []
jobs_done = 0
index = 0
jobs_len = len(jobs)
while jobs_done < jobs_len:
# νμ¬ μκ° μ μ μμ²λλ processλ heapμ λ£κΈ°
while index < jobs_len and jobs[index][0] <= current_time:
heapq.heappush(heap, (jobs[index][1], jobs[index][0]))
index += 1
# μ΅μ μκ° λΆν° μμ
μμ
if heap:
process_time, req_time = heapq.heappop(heap)
current_time += process_time
total_time += current_time - req_time
jobs_done += 1
else:
# νμ΄ λΉμ΄μλ κ²½μ°
if index < jobs_len: current_time = jobs[index][0]
else: current_time = current_time + 1
return total_time // jobs_len
μ€κ°μ μ€μ±λ μλ μ½λ μ°Έκ³ νμ¬μ μμ±νμμ΅λλ€. μ΅μ’
μ½λλ₯Ό 보λ μ€μ±λ μ½λκ° λ κΉλνκ³ ν¨μ¨μ μ΄λ€μ π ν λ€μ 볡μ΅νκ³ κ°λλΉ !
λ¬Έμ νΈλλΌ μκ³ νμ
¨μ΅λλ€ -!
@tgyuuAn # νμ¬ μκ°μ κΈ°μ€μΌλ‘ κ°±μ
while heap[0][0] != now + heap[0][1]:
temp = heappop(heap)
heappush(heap, [now + temp[1], temp[1], temp[2]])
peek = heappop(heap)
now = peek[0]
answer += peek[0] - peek[2]
keep_job = [now + duration_time, duration_time, input_time] μ¬κΈ°μ heapμ μμ
μ μΆκ°ν λ μ λ ¬ κΈ°μ€μ΄ λ¬Έμ μλ€μ ν
μ€νΈ : κΈ°μ‘΄ heap : |
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int solution(vector<vector<int>> jobs) {
sort(jobs.begin(), jobs.end());
int currentTime = 0;
int answer = 0;
auto cmp = [](const auto &a, const auto &b) {
if (a[1] == b[1]) return a[0] > b[0];
return a[1] > b[1];
};
priority_queue<vector<int>, vector<vector<int>>, decltype(cmp)> pq(cmp);
for (int i = 0; i < jobs.size();) {
while (i < jobs.size() && jobs[i][0] <= currentTime) {
pq.emplace(jobs[i]);
i++;
}
if (pq.empty()) {
currentTime = jobs[i][0] + jobs[i][1];
answer += jobs[i][1];
i++;
continue;
}
auto job = pq.top();
pq.pop();
currentTime += job[1];
answer += currentTime - job[0];
}
while (!pq.empty()) {
auto job = pq.top();
pq.pop();
currentTime += job[1];
answer += currentTime - job[0];
}
return answer / (int) jobs.size();
} νλλμ€ν¬κ° μμ μ μννκ³ μμ§ μμ λμλ λ¨Όμ μμ²μ΄ λ€μ΄μ¨ μμ λΆν° μ²λ¦¬ν©λλ€. μ΄ μ‘°κ±΄μ μ λ³΄κ³ νλ€κ° μ μ΄λ κ² μ΄λ ΅μ§?? νλλ° λμ€μ λ¬Έμ μ²μ²ν μ½μ΄λ³΄λ λ¬Έμ λ₯Ό μλͺ» νκ³ μλ€λ μ¬μ€μ κΉ¨λ¬μμ΅λλ€. μ§νν μ μλ μμ λ€ μ€ μμ μκ°μ΄ 짧μ κ²λΆν° 그리λνκ² μ²λ¦¬νλ©΄ λλ―λ‘ μ°μ μμ ν μ¬μ© λ±ν νν΄ κ±Έ κ³³μ΄ μλ κΉλν μ½λμ λλ€. |
π λ¬Έμ λ§ν¬
λμ€ν¬ 컨νΈλ‘€λ¬
βοΈ μμλ μκ°
1μκ° 10λΆ
β¨ μλ μ½λ
jobs
λ₯Ό μμ² μκ° μμΌλ‘ μ λ ¬curr_time
μ μ²λ¦¬ κ°λ₯ν λͺ¨λ μμ μ μ΅μ νμ μΆκ°wait_time
μ μ¦κ° μν¨λ€μμ² μκ° μμΌλ‘ μ λ ¬ν ν 짧μ μμ λΆν° μ²λ¦¬νλ μ΄μ
λκΈ° μκ°μ μ΅μννκΈ° μν¨μ΄λ€.
짧μ μμ μ λ¨Όμ μ²λ¦¬νλ©΄, λ€μ μμ μ λκΈ° μκ°μ΄ μ€μ΄λ¦. μμ€ν μ΄ λ λ§μ μμ μ λ λΉ λ₯΄κ² μ²λ¦¬ν μ μλ€.
[1, 2, 4]
: μ²λ¦¬ μκ°μ΄ 1, 2, 4μ΄(1 + 3) / 3 = 4 / 3 = 1.3
(4 + 5) / 3 = 9 / 3 = 3
κΈ΄ μμ λΆν° μ²λ¦¬ μ
1.3μ΄μμ 3μ΄λ‘ νκ· λκΈ° μκ°μ΄ ν¬κ² λμ΄λλ€
π μλ‘κ² μκ²λ λ΄μ©