BackmediumArrays Infosys

Optimizing Warehouse Storage Solution

Problem Statement

Given a list of crates with their respective weights and a target storage capacity, use two pointers to find the maximum total weight of crates that can be stored without exceeding the target capacity, considering the crates are arranged in a circular manner around the warehouse.

Example 1
Input
[40, 30, 10, 20, 50], 100
Output
90

Explanation: Step-by-step: We first sort the crates in descending order of their weights. Then, we initialize two pointers, one at the start and one at the end of the sorted array. We iterate through the array, adding the weights of the crates at the start and end pointers to the total weight. If the total weight exceeds the target capacity, we move the pointer of the heavier crate towards the center. Finally, we return the maximum total weight.

Example 2
Input
[40, 30, 20, 10, 50], 80
Output
80

Explanation: Step-by-step: We first sort the crates in descending order of their weights. Then, we initialize two pointers, one at the start and one at the end of the sorted array. We iterate through the array, adding the weights of the crates at the start and end pointers to the total weight. If the total weight exceeds the target capacity, we move the pointer of the heavier crate towards the center. Finally, we return the maximum total weight.

Constraints

  • 1 <= number of crates <= 10^5
  • 1 <= weight of each crate <= 10^6
  • 1 <= target storage capacity <= 10^7
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free