Suppose we have a list of numbers called nums where each value represents a group of people looking to skydive together. And we have another value k representing how many days they can apply for skydiving. We have to find the minimum capacity of the plane we need to be able to fulfill all requests within k days. The requests should be fulfilled in the order they were given and a plane can only fly once a day.
So, if the input is like nums = [16, 12, 18, 11, 13], k = 3, then the output will be 28, as 28-person airplane can group the given requests by [16, 12], , [11, 13].
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums, k): if not nums: return 0 start, end = max(nums), sum(nums) while start < end: mid = (start + end) // 2 days = 1 temp = 0 for num in nums: if temp + num > mid: days += 1 temp = num else: temp += num if days > k: start = mid + 1 else: end = mid return start ob = Solution() nums = [16, 12, 18, 11, 13] k = 3 print(ob.solve(nums, k))
[16, 12, 18, 11, 13], 3