You have some apples and a basket that can carry up to 5000 units of weight.
Given an integer array weight where weight[i] is the weight of the ith apple, return the maximum number of apples you can put in the basket.
Example 1:
Input: weight = [100,200,150,1000] Output: 4 Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.
Example 2:
Input: weight = [900,950,800,1000,700,800] Output: 5 Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.
Constraints:
1 <= weight.length <= 1031 <= weight[i] <= 103When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of apples of different weights and a basket that can only hold a certain total weight before breaking. The brute force approach involves exploring every conceivable combination of apples to see which groups fit inside the basket without exceeding the limit.
Here's how the algorithm would work step-by-step:
def max_apples_in_basket(apple_weights, weight_limit):
maximum_apple_count = 0
# Generate every possible combination size to find the maximum possible fit
for combination_size in range(1, len(apple_weights) + 1):
from itertools import combinations
for apple_group in combinations(apple_weights, combination_size):
current_total_weight = sum(apple_group)
# Validate if the current group weight stays within the basket limit
if current_total_weight <= weight_limit:
if combination_size > maximum_apple_count:
maximum_apple_count = combination_size
return maximum_apple_countTo fit the maximum number of apples into your basket, the strategy is to always pick up the lightest available apple first. By prioritizing the smallest apples, you save as much space as possible so you can carry the highest total count before the basket reaches its weight limit.
Here's how the algorithm would work step-by-step:
def max_apples_in_basket(apple_weights):
# Sorting allows us to greedily select the lightest apples first to maximize count.
sorted_apple_weights = sorted(apple_weights)
total_basket_weight = 0
maximum_apple_count = 0
maximum_weight_limit = 5000
for current_apple_weight in sorted_apple_weights:
# We stop adding when the next apple exceeds the weight capacity of the basket.
if total_basket_weight + current_apple_weight > maximum_weight_limit:
break
total_basket_weight += current_apple_weight
maximum_apple_count += 1
# Returning the count gives the maximum number of apples that fit within the weight limit.
return maximum_apple_count| Case | How to Handle |
|---|---|
| Empty array of apple weights | Return zero immediately since no apples are available to put into the basket. |
| Array containing a single apple whose weight exceeds 5000 units | Return zero because the single apple cannot fit within the basket capacity. |
| Array containing a single apple whose weight is less than or equal to 5000 units | Return one since that single valid apple can successfully fit into the basket. |
| All apples have weights larger than the maximum capacity of 5000 units | Sorting and greedy accumulation will fail to add any elements, correctly resulting in a count of zero. |
| Total sum of all apple weights is less than or equal to 5000 units | Return the total length of the array because every single apple fits inside the basket. |
| Weights include zero or negative numbers | Filter out non-positive weights or assume valid positive inputs per problem constraints so they do not incorrectly inflate the count. |
| Large input size with thousands of lightweight apples | An efficient sorting approach ensures logarithmic or linearithmic time complexity to scale well within performance limits. |
| Apple weights summing to exactly 5000 units | The greedy selection sums the exact matching weights and returns the precise count of apples used. |