leetle

Daily challenge for 2026-09-28

636. Cargo Container Unloader

You manage a port unloading crane. Shipping containers arrive sequentially, each with a given integer weight. The crane stacks containers on top of each other. However, if a newly arriving container weighs strictly more than the container directly on top of the stack, the lighter container collapses and is removed (popped) from the stack. This process repeats until the newly arriving container either rests on a container with weight greater than or equal to its own, or the stack becomes empty. The new container is then pushed onto the stack.

Given an array of positive integers weights representing the arriving containers in order, return the total number of containers remaining on the stack after all containers have arrived.

Example:
Input: weights = [5, 3, 4]
Output: 2
Explanation: First, 5 is pushed. Then 3 is pushed (stack is [5, 3]). When 4 arrives, 3 < 4 so 3 collapses. Then 5 >= 4, so 4 is placed on top of 5. Remaining stack is [5, 4], size is 2.

Run this challenge interactively in Python or JavaScript when the app loads.