In order to maximize profit and minimize cost, the...
تم الإنشاء في: ١٠ سبتمبر ٢٠٢٥
تمت الإجابة باستخدام GPT-5 Thinking بواسطة Chat01
تم الإنشاء في: ١٠ سبتمبر ٢٠٢٥
تمت الإجابة باستخدام GPT-5 Thinking بواسطة Chat01
In order to maximize profit and minimize cost, the HR has decided to create a program to filter through candidates and find suitable people to work for the company.
There are a total of n candidates, and the super advanced AI for the company has determined that for the ith candidate (starting from 0), the expected salary of that candidate is s[i] per month and the amount of work that candidate can do is w[i]. However, each candidate needs to be paid bonuses, health insurance, and other miscellaneous costs, so the monthly cost of each candidate is going to be double their expected salary.
The company has a monthly budget of b to spend on the new employees, and it's unlikely that they will be able to hire all candidates. They want to maximize the amount of work done while making sure the amount of money spent on the new employees doesn't exceed their monthly budget.
That is where you come in. You need to write a function that takes each candidate's expected salary, the amount of work they can do, and the company's monthly budget, and calculate the most work the new employees can do.
Parameters
s: A list of integers representing the expected monthly salary of each candidate.
w: A list of integers representing how much work that candidate can do if the company hires them.
b: An integer representing the company's monthly budget.
Result
An integer representing the maximum work done by the employees while not exceeding the company's budget.
Examples
Example 1:
Input: s = [2, 2, 3, 4], w = [2, 4, 4, 5], b = 15
Output: 10
Explanation: The monthly cost on each candidate is double their expected salary, so [4, 4, 6, 8]. Hiring candidates 0, 1, 2, the total cost is equal to 4 + 4 + 6 = 14 <= 15 while the total work done is 2 + 4 + 4 = 10, which is the maximum.
Restrictions
1 <= n <= 1000
1 <= s[i] <= 10^6 for each 0 <= i < n
1 <= w[i] <= 100 for each 0 <= i < n
1 <= b <= 1000
code in java and also explain the logic
Here’s a clean, efficient solution—this is exactly a 0/1 knapsack: each candidate is an item with
2 * s[i] (salary plus benefits), andw[i] (work produced),and we want to maximize total value without exceeding budget b.
Use a 1-D dynamic programming array dp[c] = maximum work achievable with budget at most c.
For each candidate (processed once), iterate c downwards from b to cost and try taking the candidate.
Time is O(n * b) (≤ 1e6 ops with given limits), space is O(b).
javapublic class HiringMaxWork { // Returns the maximum work achievable within budget b public static int maxWork(int[] s, int[] w, int b) { if (s == null || w == null || s.length != w.length) { throw new IllegalArgumentException("s and w must be non-null and of equal length"); } int n = s.length; int[] dp = new int[b + 1]; // dp[c] = max work with budget at most c for (int i = 0; i < n; i++) { int cost = 2 * s[i]; // monthly cost including extras if (cost > b) continue; // can't afford this candidate at all // Traverse backwards to preserve 0/1 choice (each candidate at most once) for (int c = b; c >= cost; c--) { dp[c] = Math.max(dp[c], dp[c - cost] + w[i]); } } // dp[b] already represents the best we can do with budget at most b return dp[b]; } // Quick demo with the provided example public static void main(String[] args) { int[] s = {2, 2, 3, 4}; int[] w = {2, 4, 4, 5}; int b = 15; System.out.println(maxWork(s, w, b)); // prints 10 } }
dp[c] always holds the best work for any combination costing ≤ c, so returning dp[b] gives the optimal answer within the budget.2 * s[i] > b are ignored since they can’t be hired under the budget.For s = [2,2,3,4], costs are [4,4,6,8], w = [2,4,4,5], b = 15.
Optimal is hiring 0,1,2: cost 4+4+6=14 ≤ 15, work 2+4+4=10. The code returns 10.