Problem Statement:
An Amazon warehouse processes n shipments, where each shipment affects the inventory levels. These effects are provided in the inventoryAdjustments array, with positive values representing restocks and negative values indicating items taken out for orders.
Starting with zero inventory level, the goal is to process shipments in an optimal order to maximize the number of shipments completed while maintaining a strictly positive inventory level. If the inventory reaches zero or goes negative, operations halt, and no further shipments can be processed.
Given an integer array inventoryAdjustments of length n, find the maximum number of shipments that can be processed without depleting the inventory.
Problem Statement:
You're managing Amazon's cloud storage system. There are n tasks that need to be processed, and the $i^{th}$ task is broken into num_segments[i] segments. The cloud storage is divided into m storage units. The segments are allocated to these units with the following rules:
Each segment is stored in exactly one storage unit.
Each storage unit must hold at least one segment.
A storage unit cannot contain segments from more than one task.
The storage efficiency is measured by the minimum number of segments that any storage unit contains. Your task is to organize the segments in such a way that this storage efficiency is maximized.
Problem1 Solution
Maximize Inventory Adjustments Solution
Topics Involved / Prerequisites
Greedy Algorithms
Array Sorting
Overview
Approach
1. Segregating and Building Since we are allowed to process the shipments in an optimal order, we should immediately grab all the positive adjustments. We iterate through the array, adding all positive values to our running inventory total and incrementing our processed count. We store all the negative adjustments in a separate list.
2. Fulfilling Safely Once our inventory is at its absolute maximum, we sort our list of negative adjustments in ascending order based on their absolute value. This allows us to fulfill the "cheapest" shipments first. We subtract them from our inventory one by one. If subtracting a shipment would cause the inventory to drop to 0 or below, we immediately halt and return our current count, as the rules require a strictly positive inventory.
Time Complexity
Time: O(Nlog N) - Sorting the negative adjustments is the bottleneck. The array traversals are strictly linear.
Problem2 -Maximum Storage Efficiency Solution
Topics Involved / Prerequisites
Binary Search on Answer
Greedy Math
Overview
Approach
1. Binary Search the Answer
The lowest possible storage efficiency is 1, and the absolute maximum is the size of the smallest task (since a task must have at least one unit, the highest minimum load it can have is all of its segments in a single unit). We can binary search an efficiency target
Xwithin this range.2. Validating the Threshold
For a guessed efficiency
X, how many units can a task utilize without any single unit dropping belowXsegments? The math is exactly floor (segments / X ). We calculate this maximum allowed unit count for every task and sum them up. If any task gets 0 units, the guess is invalid. If the total allowed units across all tasks is >= m, then the thresholdXis achievable!Time Complexity
Time: O(N log S) - Where S is the minimum value in the
num_segmentsarray. We perform an O(N) check for every step of our binary search.Space: O(1) - We only use a few basic variables to track our search bounds and unit sums.