Similar Problems
Similar Problems not available
Maximum Number Of Robots Within Budget - Leetcode Solution
Companies:
LeetCode: Maximum Number Of Robots Within Budget Leetcode Solution
Difficulty: Hard
Topics: binary-search sliding-window heap-priority-queue array prefix-sum
Problem:
You are given a list of robots where each robot consists of three integers: robotId, biteFactor, and price. The biteFactor represents the number of people a robot can bite in one minute. The price represents the cost of purchasing the robot. You have a budget B and you want to purchase as many robots as possible within the budget. However, there is a constraint that no two robots with the same bite factor can be purchased. You need to find the maximum number of robots you can purchase within the given budget.
Solution:
The problem essentially boils down to a constrained knapsack problem, where the weight limit is the budget B and the bite factor is the weight of the items. We can use dynamic programming to solve this problem.
First, we sort the robots in decreasing order of bite factor, so that if we have to make a choice between two robots with same bite factor, we choose the more expensive one (this is the constraint mentioned in the problem). Next, we define a 2D array dp where dp[i][j] represents the maximum number of robots that can be purchased with a budget of j and considering only the first i robots.
Then we can fill up the dp array iteratively as follows:
For any robot i, if its price is greater than j, then there is no way we can buy this robot, and we simply set dp[i][j] = dp[i-1][j], which means we move to the next robot without considering the current one.
If the price of robot i is less than or equal to j, then we have two choices: either buy the robot or skip it. If we choose to buy the robot, then we add its bite factor to the total weight, so we need to subtract the bite factor from j while calculating the maximum number of robots. Therefore, if the current weight (j) is greater than or equal to the weight of the current robot (biteFactor[i]), then we have two choices.
a. Buy the robot: In this case, we add 1 to the result and subtract the price and bite factor of the robot from the current weight j. This means, we consider the maximum number of robots up to index i-1 with remaining budget j - price[i] and weight limit j - biteFactor[i], and add the current robot to the result. So the value of dp[i][j] would be dp[i-1][j-price[i] - biteFactor[i]] + 1.
b. Skip the robot: In this case, we simply move on to the next robot without buying the current one. So the value of dp[i][j] would be dp[i-1][j].
Finally, we return the value of dp[n][B], where n is the number of robots.
Time Complexity: O(nB), where n is the number of robots and B is the budget.
Space Complexity: O(nB), where n is the number of robots and B is the budget.
Python Code:
def maxRobotsWithinBudget(robots, B): robots.sort(reverse=True, key=lambda x: (x[1], -x[2])) n = len(robots) dp = [[0] * (B+1) for _ in range(n+1)]
for i in range(1, n+1):
robotId, biteFactor, price = robots[i-1]
for j in range(1, B+1):
if price > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = max(dp[i-1][j-price-biteFactor] + 1, dp[i-1][j])
return dp[n][B]
Maximum Number Of Robots Within Budget Solution Code
1