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

39-pknujsp #163

Merged
merged 4 commits into from
May 3, 2024
Merged

39-pknujsp #163

merged 4 commits into from
May 3, 2024

Conversation

pknujsp
Copy link
Collaborator

@pknujsp pknujsp commented Mar 19, 2024

πŸ”— 문제 링크

λ””μŠ€ν¬ 컨트둀러

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

1μ‹œκ°„ 10λΆ„

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

λͺ¨λ“  μž‘μ—…μ˜ μš”μ²­λΆ€ν„° μ’…λ£ŒκΉŒμ§€ κ±Έλ¦° μ‹œκ°„μ˜ 평균을 μ΅œμ†Œν™”ν•˜κΈ°

  • jobsλ₯Ό μš”μ²­ μ‹œκ°„ 순으둜 μ •λ ¬
  • 남은 μž‘μ—…μ΄ 없을 λ•Œ κΉŒμ§€ λ‹€μŒ 과정을 λ°˜λ³΅ν•œλ‹€
    • ν˜„μž¬ μ‹œκ°„curr_time 에 처리 κ°€λŠ₯ν•œ λͺ¨λ“  μž‘μ—…μ„ μ΅œμ†Œ νž™μ— μΆ”κ°€
      • μ‹€ν–‰ μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ νž™μ— μΆ”κ°€
      • μ‹€ν–‰ μ‹œκ°„μ΄ 짧은 μž‘μ—… λΆ€ν„° λ¨Όμ € 처리 ν•˜λ„λ‘ ν•œλ‹€
    • νž™μ— μž‘μ—…μ΄ μžˆλ‹€λ©΄, μΆ”μΆœν•˜μ—¬ ν•΄λ‹Ή μž‘μ—…μ„ μ²˜λ¦¬ν•œλ‹€
      • ν˜„μž¬ μ‹œκ°„μ„ μž‘μ—…μ˜ μ‹€ν–‰ μ‹œκ°„ 만큼 증가 μ‹œν‚¨λ‹€
      • μž‘μ—…μ˜ λŒ€κΈ° μ‹œκ°„λ§ŒνΌ wait_time을 증가 μ‹œν‚¨λ‹€
    • μž‘μ—…μ΄ μ—†λ‹€λ©΄, 1초 λ’€λ‘œ μ΄λ™ν•œλ‹€

μš”μ²­ μ‹œκ°„ 순으둜 μ •λ ¬ν•œ ν›„ 짧은 μž‘μ—…λΆ€ν„° μ²˜λ¦¬ν•˜λŠ” 이유

λŒ€κΈ° μ‹œκ°„μ„ μ΅œμ†Œν™”ν•˜κΈ° μœ„ν•¨μ΄λ‹€.
짧은 μž‘μ—…μ„ λ¨Όμ € μ²˜λ¦¬ν•˜λ©΄, λ‹€μŒ μž‘μ—…μ˜ λŒ€κΈ° μ‹œκ°„μ΄ 쀄어듦. μ‹œμŠ€ν…œμ΄ 더 λ§Žμ€ μž‘μ—…μ„ 더 λΉ λ₯΄κ²Œ μ²˜λ¦¬ν•  수 μžˆλ‹€.

  • ex) [1, 2, 4] : 처리 μ‹œκ°„μ΄ 1, 2, 4초
    • 짧은 μž‘μ—…λΆ€ν„° 처리 : (1 + 3) / 3 = 4 / 3 = 1.3
    • κΈ΄ μž‘μ—…λΆ€ν„° 처리 : (4 + 5) / 3 = 9 / 3 = 3

κΈ΄ μž‘μ—…λΆ€ν„° 처리 μ‹œ
1.3μ΄ˆμ—μ„œ 3초둜 평균 λŒ€κΈ° μ‹œκ°„μ΄ 크게 λŠ˜μ–΄λ‚œλ‹€

def solution(jobs):
    jobs = deque(sorted(jobs))
    jobs_num = len(jobs) 
    
    curr_time = wait_time = 0
    heap = []
    
    while heap or jobs:
        while jobs and jobs[0][0] <= curr_time:
            # ν˜„μž¬ μ‹œκ° 내에 μ²˜λ¦¬κ°€λŠ₯ν•œ μž‘μ—…μ„ λͺ¨λ‘ νž™μ— μΆ”κ°€
            requested_time, duration = jobs.popleft()
            heappush(heap, (duration, requested_time))
            
        if heap:
            duration, requested_time = heappop(heap)
            
            curr_time += duration
            wait_time += curr_time - requested_time
        else:
            curr_time += 1
    
    return wait_time // jobs_num

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

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.

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이 λ‚˜μ˜€λ„€μš”... μ™œμ£  γ… γ… ... ν•˜..

Copy link
Collaborator

@H0ngJu H0ngJu left a 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

쀑간에 μ€€μ„±λ‹˜ μˆ˜λ„ μ½”λ“œ μ°Έκ³ ν•˜μ—¬μ„œ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€. μ΅œμ’… μ½”λ“œλ₯Ό λ³΄λ‹ˆ μ€€μ„±λ‹˜ μ½”λ“œκ°€ 더 κΉ”λ”ν•˜κ³  νš¨μœ¨μ μ΄λ„€μš” πŸ™‚ νž™ λ‹€μ‹œ λ³΅μŠ΅ν•˜κ³  κ°‘λ‹ˆλ‹Ή !
λ¬Έμ œν‘ΈλŠλΌ μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€ -!

@pknujsp
Copy link
Collaborator Author

pknujsp commented Mar 25, 2024

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

# 데이터 λ“€μ–΄μ˜€λ©΄ νž™μœΌλ‘œ μ§‘μ–΄λ„£μŒ.
# κΈ°λ‹€λ¦¬λŠ” μ‹œκ°„ + μ§„ν–‰λ˜λŠ” μ‹œκ°„μ΄ κ°€μž₯ 적은 것 λΆ€ν„° μ‹€ν–‰μ‹œν‚΄.
jobs = [[0, 6], [2, 8], [3, 7], [7, 1], [11, 11], [19, 25], [30, 15], [32, 6], [40, 3]]
answer : 19

@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]

μ—¬κΈ°μ„œ [now +duration_time, duration_time, input_time] λ₯Ό
[input_time + duration_time, duration_time, input_time] 으둜 λ°”κΎΈλ©΄ λ˜λ„€μš”.

heap에 μž‘μ—…μ„ μΆ”κ°€ν•  λ•Œ μ •λ ¬ 기쀀이 λ¬Έμ œμ˜€λ„€μš”
기쀀이 μž‘μ—… μ†Œμš” μ‹œκ°„μ΄ λ˜μ•Όν•˜λŠ”λ° μ§€κΈˆμ€ nowλ₯Ό λ”ν•˜λ©΄μ„œ μ˜ˆμƒ μ’…λ£Œ μ‹œκ°„μ΄ 기쀀이 λ˜μ–΄λ²„λ¦¬λŠ” κ±° κ°™μ•„μš”

ν…ŒμŠ€νŠΈ : [[0, 6], [2, 8], [3, 7], [7, 1], [11, 11], [19, 25], [30, 15], [32, 6], [40, 3]]

κΈ°μ‘΄ heap : [[39, 25, 19], [48, 15, 30], [42, 3, 40]]
μˆ˜μ • : [[43, 3, 40], [45, 15, 30], [44, 25, 19]]

@MunbinLee
Copy link
Contributor

MunbinLee commented Apr 21, 2024

#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();
}

ν•˜λ“œλ””μŠ€ν¬κ°€ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κ³  μžˆμ§€ μ•Šμ„ λ•Œμ—λŠ” λ¨Όμ € μš”μ²­μ΄ λ“€μ–΄μ˜¨ μž‘μ—…λΆ€ν„° μ²˜λ¦¬ν•©λ‹ˆλ‹€.

이 쑰건을 μ•ˆ 보고 ν’€λ‹€κ°€ μ™œ μ΄λ ‡κ²Œ 어렡지?? ν–ˆλŠ”λ°

λ‚˜μ€‘μ— 문제 천천히 μ½μ–΄λ³΄λ‹ˆ 문제λ₯Ό 잘λͺ» ν’€κ³  μžˆλ‹€λŠ” 사싀을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€.

진행할 수 μžˆλŠ” μž‘μ—…λ“€ 쀑 μ†Œμš” μ‹œκ°„μ΄ 짧은 것뢀터 κ·Έλ¦¬λ””ν•˜κ²Œ μ²˜λ¦¬ν•˜λ©΄ λ˜λ―€λ‘œ μš°μ„ μˆœμœ„ 큐 μ‚¬μš©

λ”±νžˆ νƒœν΄ κ±Έ 곳이 μ—†λŠ” κΉ”λ”ν•œ μ½”λ“œμž…λ‹ˆλ‹€.

@MunbinLee MunbinLee merged commit 83f9c41 into main May 3, 2024
1 check passed
@MunbinLee MunbinLee deleted the 39-pknujsp branch May 3, 2024 15:34
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.

4 participants