![]() |
VOOZH | about |
Given an integer N, representing number of projects, two arraysP[] and C[], consisting of N integers, and two integers W and K where, W is the initial capital amount, P[i] and C[i] are the profits and capital required to choose the ith project. The task is to calculate the maximum amount of capital required to choose at most K projects, such that the profit of the chosen projects is added to W and choosing any project required at least C[i].
Examples:
Input: N = 3, W = 0, K = 2, P[] = {1, 2, 3}, C[] = {0, 1, 1}
Output: 4
Explanation:
Project 1: With the given amount of W as 0, the 0th project can be chosen at a cost of C[0] i.e., 0, and after finishing the capital added to W i.e., W becomes W + P[0] = 1.
Project 2: With the given amount of W as 1, the 2nd project can be chosen at a cost of C[2] i.e., 1, and after finishing the capital added to W i.e., W becomes W + P[2] = 1 + 3 = 4.Input: N = 3, W = 1, K = 1, P[] = {10000, 2000, 3000}, C[] = {1, 1, 1}
Output: 10001
Approach: The given problem can be solved using the Greedy Algorithm and priority queue. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
4
Time Complexity: O((N + K) * log N)
Auxiliary Space: O(N)