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

디스크 컨트롤러 #409

Closed
Tracked by #396
fkdl0048 opened this issue Nov 26, 2024 · 0 comments
Closed
Tracked by #396

디스크 컨트롤러 #409

fkdl0048 opened this issue Nov 26, 2024 · 0 comments
Assignees
Milestone

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Nov 26, 2024

#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

struct Job {
    int start;
    int length;
    int index;
    
    Job(int s, int l, int i) : start(s), length(l), index(i) {}
};

struct CompareJob {
    bool operator()(const Job& a, const Job& b) {
        if (a.length != b.length) return a.length > b.length;
        if (a.start != b.start) return a.start > b.start;
        return a.index > b.index;
    }
};

int solution(vector<vector<int>> jobs) {
    vector<Job> job_list;
    for (int i = 0; i < jobs.size(); i++) {
        job_list.push_back(Job(jobs[i][0], jobs[i][1], i));
    }
    sort(job_list.begin(), job_list.end(), 
         [](const Job& a, const Job& b) { return a.start < b.start; });
    
    priority_queue<Job, vector<Job>, CompareJob> waiting_queue;
    int current_time = 0;
    int total_turnaround = 0;
    int job_index = 0;
    
    while (job_index < jobs.size() || !waiting_queue.empty()) {
        // 현재 시점까지 요청된 모든 작업을 대기 큐에 추가
        while (job_index < jobs.size() && job_list[job_index].start <= current_time) {
            waiting_queue.push(job_list[job_index]);
            job_index++;
        }
        
        if (waiting_queue.empty()) {
            // 대기 큐가 비어있다면 다음 작업의 시작 시간으로 이동
            current_time = job_list[job_index].start;
            continue;
        }
        
        // 우선순위가 가장 높은 작업 실행
        Job current_job = waiting_queue.top();
        waiting_queue.pop();
        
        // 작업 완료 및 반환 시간 계산
        current_time += current_job.length;
        total_turnaround += current_time - current_job.start;
    }
    
    return total_turnaround / jobs.size();
}
@fkdl0048 fkdl0048 self-assigned this Nov 26, 2024
@fkdl0048 fkdl0048 added this to Todo Nov 26, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Nov 26, 2024
@fkdl0048 fkdl0048 added this to the Programmers milestone Nov 26, 2024
@fkdl0048 fkdl0048 closed this as completed Dec 2, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Todo Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

1 participant