BackmediumTwo Pointers Oracle

Galactic Resource Allocation 4 Solution

Problem Statement

Given a list of crates with weights and volumes, find the partition point where the total weight and volume of the crates are equal.

Example 1
Input
[{'weight': 50, 'volume': 50}, {'weight': 50, 'volume': 50}, {'weight': 20, 'volume': 20}, {'weight': 40, 'volume': 40}]
Output
4

Explanation: Step 1: Sort the crates based on their weights and volumes. In this case, the sorted crates are [{'weight': 20, 'volume': 20}, {'weight': 40, 'volume': 40}, {'weight': 50, 'volume': 50}, {'weight': 50, 'volume': 50}]. Step 2: Initialize two pointers, one at the start and one at the end of the sorted list. In this case, the pointers are at the first and last elements of the list. Step 3: Calculate the total weight and volume of the crates from the start pointer to the end pointer. In this case, the total weight and volume are 100 and 100, respectively. Step 4: Since the total weight and volume of the crates from the start pointer to the end pointer are equal, return the end pointer, which is 4.

Example 2
Input
[{'weight': 30, 'volume': 30}, {'weight': 30, 'volume': 30}, {'weight': 40, 'volume': 40}]
Output
4

Explanation: Step 1: Sort the crates based on their weights and volumes. In this case, the sorted crates are [{'weight': 30, 'volume': 30}, {'weight': 30, 'volume': 30}, {'weight': 40, 'volume': 40}]. Step 2: Initialize two pointers, one at the start and one at the end of the sorted list. In this case, the pointers are at the first and last elements of the list. Step 3: Calculate the total weight and volume of the crates from the start pointer to the end pointer. In this case, the total weight and volume are 100 and 100, respectively. Step 4: Since the total weight and volume of the crates from the start pointer to the end pointer are equal, return the end pointer, which is 4.

Constraints

  • 1 <= number of crates <= 100
  • -1000 <= weight of a crate <= 1000
  • -1000 <= volume of a crate <= 1000
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