|
| 1 | +/* |
| 2 | +Award Budget Cuts |
| 3 | +The awards committee of your alma mater (i.e. your college/university) asked for your assistance with a budget allocation problem they’re facing. Originally, |
| 4 | +the committee planned to give N research grants this year. However, due to spending cutbacks, |
| 5 | +the budget was reduced to newBudget dollars and now they need to reallocate the grants. |
| 6 | +The committee made a decision that they’d like to impact as few grant recipients as possible by applying a maximum cap on all grants. |
| 7 | +Every grant initially planned to be higher than cap will now be exactly cap dollars. Grants less or equal to cap, obviously, won’t be impacted. |
| 8 | +
|
| 9 | +Given an array grantsArray of the original grants and the reduced budget newBudget, write a function findGrantsCap that finds in the most efficient manner a cap such that the least number of recipients is impacted and that the new budget constraint is met (i.e. sum of the N reallocated grants equals to newBudget). |
| 10 | +
|
| 11 | +Analyze the time and space complexities of your solution. |
| 12 | +
|
| 13 | +Example: |
| 14 | +
|
| 15 | +input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190 |
| 16 | +
|
| 17 | +output: 47 # and given this cap the new grants array would be |
| 18 | + # [2, 47, 47, 47, 47]. Notice that the sum of the |
| 19 | + # new grants is indeed 190 |
| 20 | +Constraints: |
| 21 | +
|
| 22 | +[time limit] 5000ms |
| 23 | +
|
| 24 | +[input] array.double grantsArray |
| 25 | +
|
| 26 | +0 ≤ grantsArray.length ≤ 20 |
| 27 | +0 ≤ grantsArray[i] |
| 28 | +[input] double newBudget |
| 29 | +
|
| 30 | +[output] double |
| 31 | +*/ |
| 32 | + |
| 33 | +var cutAwardBadges = function(nums, newBadge) { |
| 34 | + var currentBadge = 0; |
| 35 | + for(var i = 0; i < nums.length; i++) |
| 36 | + currentBadge += nums[i]; |
| 37 | + |
| 38 | + if(currentBadge < newBadge) |
| 39 | + return; |
| 40 | + |
| 41 | + const cap = findCap(nums, currentBadge, newBadge); |
| 42 | + |
| 43 | + var iter = 0; |
| 44 | + while(iter >= 0 && nums[iter] > cap) { |
| 45 | + nums[iter] = cap; |
| 46 | + iter++; |
| 47 | + } |
| 48 | + |
| 49 | + return nums; |
| 50 | +} |
| 51 | + |
| 52 | +var findCap = function(nums, currentBadge, newBadge) { |
| 53 | + nums.sort(function(a, b) { return b - a }); |
| 54 | + if(nums[nums.length - 1] * nums.length > newBadge) |
| 55 | + return newBadge / nums.length; |
| 56 | + |
| 57 | + var diff = currentBadge - newBadge; |
| 58 | + var iter = 0; |
| 59 | + while(iter < nums.length - 1 && diff > 0) { |
| 60 | + const substraction = nums[iter] - nums[iter + 1] |
| 61 | + diff -= (iter + 1) * substraction; |
| 62 | + iter++; |
| 63 | + } |
| 64 | + |
| 65 | + return nums[iter] + (-diff) / iter; |
| 66 | +} |
| 67 | + |
| 68 | +var main = function() { |
| 69 | + console.log(cutAwardBadges([2, 100, 50, 120, 1000], 190)); |
| 70 | + console.log(cutAwardBadges([2, 100, 50, 120, 1000], 5)); |
| 71 | +} |
| 72 | + |
| 73 | +module.exports.main = main; |
0 commit comments