FastPrepFastPrep
Problem Brief

Min Cost to Complete All Projects

FULLTIMEOA

A customer has posted several web development projects on a freelancing platform, and various web developers have put in bids for these projects. Given the bid amounts and their corresponding projects, what is the minimum amount the customer can pay to have all the projects completed?

Note: If any project has no bids, return -1.

Function Description

Complete the function minCost in the editor.

minCost has the following parameters:

  1. 1. int numProjects: the total number of projects posted by the client (labeled from 0 to n)
  2. 2. int projectId[n]: the projects that the freelancers bid on
  3. 3. int bid[n]: the bid amounts posted by the freelancers

Returns

long: the minimum cost the client can spend to complete all projects, or -1 if any project has no bids.

1Example 1

Input
numProjects = 3, projectId = [2, 0, 1, 2], bid = [8, 7, 6, 9]
Output
21
Explanation

There is only one choice of who to hire for project 0, and it will cost 7. Likewise, there is only one choice for project 1, which will cost 6. For project 2, it is optimal to hire the first web developer, instead of the fourth, and doing so will cost 8. So the final answer is 7 + 6 + 8 = 21.

If instead there were n = 4 projects, the answer would be -1 since there were no bids received on the fourth project.

2Example 2

Input
numProjects = 2, projectId = [0, 1, 0, 1, 1], bid = [4, 74, 47, 744, 7]
Output
11
Explanation

The optimal solution is to hire the first web developer to complete project 0 (which costs 4) and to hire the fifth web developer to complete project 1 (which costs 7). So the total cost for the customer will be 4 + 7 = 11.

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ numProjects, n ≤ 5x10^5
  • 0 ≤ projectId[i] < n
  • 1 ≤ bid[i] ≤ 10^9
public long minCost(int numProjects, int[] projectId, int[] bid) {
  // write your code here
}
Input

numProjects

3

projectId

[2, 0, 1, 2]

bid

[8, 7, 6, 9]

Output

21

Sign in to submit your solution.