Natarajan's Birthday Bombs
It’s Natarajan's birthday today and he has N friends.
Friends are numbered [0, 1, 2, ...., N-1] and ith friend have a positive strength B[i].
Today being his birthday, his friends have planned to give him birthday bombs (kicks).
Tushar's friends know Tushar's pain bearing limit and would hit accordingly.
If Tushar’s resistance is denoted by A (>=0) then find the lexicographically smallest order of friends to kick Tushar so that the cumulative kick strength (sum of the strengths of friends who kicks) doesn’t exceed his resistance capacity and total no. of kicks hit are maximum.
Also note that each friend can kick unlimited number of times (If a friend hits x times, his strength will be counted x times)
Return the lexicographically smallest array of maximum length where the ith index represents the index of the friend who will hit.
Example:
Example 2:
Solution
This solution is similar to the combination sum problem on leetcode.
Space Optimized Dynamic Programming
Like other unbounded knapsack problems. We have a DP array of total + 1. In addition to the DP array, we also have an index array which will store indexes of kicks. e.g. indexes[i] denotes that the ith person kicked last when the total was i. Using this array, after we're done with the DP solution, we can look back and index[totalPainTolerance] will give you the last person (index) to kick. We can substract the value and look at indexes[totalPainTolerance - kickStrengths[i]]
Non-DP Solution
Find the minimum number in the kickStrengths array.
We know that if
totalTolerance / minKickStrength
is 0, that is the max amount of kicks we can hit.We calculate the available buffer we have after we can hit with the minKickStrength max times. This can easily be calculated by
totalTolerance % minKickStrength
. e.g. if the total tolerance is 14 and the the kicks array is [3,4]. We know that we can hit with strength 3 at least 4 times to get 12. And we have a buffer of 2.We loop once through the strengths array. This works because we need to just return the max kicks in sorted order. If the current number is the minNumber, you just add the index to the results. If the current number is > minNumber, you calculate the difference (currentNumber - minNumber) and substract that value from the available buffer.
Last updated
Was this helpful?