Round 1
Questions: 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.
Example numProjects = 3 projects. projectId = [2, 0, 1, 2] bid = [8, 7, 6, 9].
- projectId[i] is aligned with bid[i].
- The first web developer bid 8 currency units for project 2.
- The second web developer bid 7 currency units for project 0.
- The third web developer bid 6 currency units for project 1.
- The fourth web developer bid 9 currency units for project 2.
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.
Function Description Complete the function minCost in the editor below.
minCost has the following parameters:
- int numProjects: the total number of projects posted by the client (labeled from 0 to n)
- int projectId[n]: the projects that the freelancers bid on
- 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.
Constraints:
- 1 ≤ numProjects, n ≤ 5×10^5
- 0 ≤ projectId[i] < n
- 1 ≤ bid[i] ≤ 10^9
Sample Case 0 Sample Input
STDIN Function ----- ----- 2 → numProjects = 2 5 → projectId[] size n = 5 0 → projectId = [0, 1, 0, 1, 1] 1 0 1 1 5 → bid[] size n = 5 4 → bid = [4, 74, 47, 744, 7] 74 47 744 7
Sample Output
11
Explanation The bids are as follows:
- The first web developer bid 4 currency units for project 0.
- The second web developer bid 74 currency units for project 1.
- The third web developer bid 47 currency units for project 0.
- The fourth web developer bid 744 currency units for project 1.
- The fifth web developer bid 7 currency units for project 1.
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.
Working Solution
public static int minimumAmountToPay(int numProjects, int[] projectid, int[] bid) { // Map to store the minimum bid for each project Map<Integer, Integer> minBidMap = new HashMap<>(); // Process each bid for (int i = 0; i < projectid.length; i++) { int project = projectid[i]; int bidAmount = bid[i]; // Update the minimum bid for this project if (minBidMap.containsKey(project)) { minBidMap.put(project, Math.min(minBidMap.get(project), bidAmount)); } else { minBidMap.put(project, bidAmount); } } // Calculate the total minimum amount int totalMinAmount = 0; for (int project = 0; project < numProjects; project++) { if (!minBidMap.containsKey(project)) { // If any project has no bids, return -1 return -1; } totalMinAmount += minBidMap.get(project); } return totalMinAmount; }
Candidate's Approach
No approach provided.
Interviewer's Feedback
No feedback provided.