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

Total Cost to Hire K Workers #132

Closed
Tracked by #101
fkdl0048 opened this issue Oct 17, 2024 · 0 comments
Closed
Tracked by #101

Total Cost to Hire K Workers #132

fkdl0048 opened this issue Oct 17, 2024 · 0 comments
Assignees
Labels
Milestone

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Oct 17, 2024

#include <iostream>
#include <vector>
#include <queue>
#include <tuple>

class Solution {
public:
    long long totalCost(std::vector<int>& costs, int k, int candidates) {
        int n = costs.size();
        long long totalCost = 0;
        int left = 0;
        int right = n - 1;

        // 최소 힙 정의: 비용, 인덱스
        using Worker = std::pair<int, int>;
        auto cmp = [](const Worker& a, const Worker& b) {
            if (a.first == b.first)
                return a.second > b.second; // 비용이 같으면 인덱스가 작은 순
            return a.first > b.first; // 비용이 작은 순
        };
        std::priority_queue<Worker, std::vector<Worker>, decltype(cmp)> leftHeap(cmp);
        std::priority_queue<Worker, std::vector<Worker>, decltype(cmp)> rightHeap(cmp);

        // 초기 힙 구성
        for (; left < candidates && left <= right; ++left) {
            leftHeap.emplace(costs[left], left);
        }
        for (; right >= n - candidates && right >= left; --right) {
            rightHeap.emplace(costs[right], right);
        }

        // k번의 고용 세션 진행
        for (int i = 0; i < k; ++i) {
            if (leftHeap.empty()) {
                auto [cost, idx] = rightHeap.top();
                rightHeap.pop();
                totalCost += cost;

                if (right >= left) {
                    rightHeap.emplace(costs[right], right);
                    --right;
                }
            }
            else if (rightHeap.empty()) {
                auto [cost, idx] = leftHeap.top();
                leftHeap.pop();
                totalCost += cost;

                if (left <= right) {
                    leftHeap.emplace(costs[left], left);
                    ++left;
                }
            }
            else {
                // 두 힙에서 최솟값 비교
                auto [leftCost, leftIdx] = leftHeap.top();
                auto [rightCost, rightIdx] = rightHeap.top();

                if (leftCost < rightCost || (leftCost == rightCost && leftIdx < rightIdx)) {
                    leftHeap.pop();
                    totalCost += leftCost;

                    if (left <= right) {
                        leftHeap.emplace(costs[left], left);
                        ++left;
                    }
                }
                else {
                    rightHeap.pop();
                    totalCost += rightCost;

                    if (right >= left) {
                        rightHeap.emplace(costs[right], right);
                        --right;
                    }
                }
            }
        }

        return totalCost;
    }
};
@fkdl0048 fkdl0048 mentioned this issue Oct 17, 2024
75 tasks
@fkdl0048 fkdl0048 self-assigned this Oct 17, 2024
@fkdl0048 fkdl0048 added this to Todo Oct 17, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Oct 17, 2024
@fkdl0048 fkdl0048 moved this from Todo to Two-Week Plan in Todo Oct 17, 2024
@fkdl0048 fkdl0048 added this to the LeetCode milestone Oct 17, 2024
@github-project-automation github-project-automation bot moved this from Two-Week Plan to Done in Todo Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

1 participant