Problem 375057 · medium · Phase 03 Linear Management & Searching

Shared Elevator Trips

sorting · greedy · two pointers

A freight elevator carries at most two crates per trip and at most limit kilograms in total. You are given the crate weights weights (each individually at most limit). Return the minimum number of trips needed to move every crate.

Examples

Input:  weights = [3, 2, 2, 1], limit = 3
Output: 3
Explanation: Trips (1, 2), (2) and (3).
Input:  weights = [3, 5, 3, 4], limit = 5
Output: 4
Explanation: No two crates fit together.

Constraints

  • 0 <= len(weights) <= 10**5
  • 1 <= weights[i] <= limit <= 3 * 10**4
  • Target complexity: O(n log n).

Goals

  • Sort weights and pair the heaviest with the lightest when possible
  • Move two pointers inward while counting trips
  • Justify the greedy choice
Starting Python…