Cargo Port Ship Stops
Updated
Problem statement
Cargo items wait in a line at a home port. Item i weighs weights[i] and must be delivered to port ports[i]. The delivery ports are numbered from 1 to portCount.
A ship carries the items in trips. On each trip:
- The ship loads items from the front of the line, one at a time and in order. It keeps loading while it holds fewer than
maxItemsitems and the next item keeps the total weight at mostmaxWeight. Loading stops at the first item that does not fit; no later item is loaded ahead of it. - The ship stops once at each different port that its items go to. Several items for the same port need only one stop there.
- The ship returns to the home port. This counts as one more stop.
Trips go on until the line is empty. Return the total number of stops.
The arrays weights and ports may have different lengths. Only the first k positions describe items, where k is the length of the shorter array. Extra values at the end of the longer array are ignored.
Function
countShipStops(weights: int[], ports: int[], portCount: int, maxItems: int, maxWeight: int) → intExamples
Example 1
weights = [40,25,60,10,30]ports = [2,1,2,3,1]portCount = 3maxItems = 3maxWeight = 100return = 7Trip 1 loads 40 and 25 (total 65). The next item weighs 60, and 65 + 60 = 125 is more than 100, so the ship leaves. It stops at ports 2 and 1, then returns home: 3 stops. Trip 2 loads 60, 10 and 30 (total exactly 100, and 3 items). It stops at ports 2, 3 and 1, then returns home: 4 stops. The answer is 3 + 4 = 7.
Example 2
weights = [5,5,5,5]ports = [1,1,2]portCount = 2maxItems = 2maxWeight = 50return = 4ports has 3 values and weights has 4, so only the first 3 items exist; the last weight is ignored. Trip 1 loads the first two items (the item limit is 2). Both go to port 1, so it is 1 stop plus the return: 2 stops. Trip 2 loads the third item for port 2: 1 stop plus the return, 2 stops. The answer is 4.
Example 3
weights = [9,9,9]ports = [1,1,1]portCount = 1maxItems = 3maxWeight = 10return = 6Two items together weigh 18, more than 10, so every trip carries one item. Each trip stops at port 1 and returns home: 2 stops. Three trips give 6 stops. Port 1 is counted again on every trip that visits it.
Constraints
1 <= weights.length <= 10^41 <= ports.length <= 10^41 <= portCount <= 10^41 <= ports[i] <= portCount1 <= maxItems <= 10^41 <= weights[i] <= maxWeight <= 10^9- The two arrays may have different lengths.
More Nutanix Online Assessment questions
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- Number of Players (for MTS2)OA · Seen Oct 2024
- Work Schedules (for MTS2)OA · Seen Oct 2024
- Minimum Operations to Convert ArrayOA · Seen Apr 2024
- No Adjacent CharactersOA · Seen Apr 2024
- Build a BST From a ListOA · Seen Nov 2022
- Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022
Similar questions at other companies
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Calculate Portfolio Rebalancing DeltasAkuna Capital · OAAkuna Capital interview questions
- Filter Transaction Logs and Calculate Reliability MetricsStripe · OAStripe interview questions
- Get Total RequestsAmazon · OA
- Balloon Color PairsInstacart · OAInstacart interview questions
- Command Frequency CounterUber · OAUber interview questions
- Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
- Process Banking OperationsVercel · OA
- First Completed Mingo LineEpic · OAEpic interview questions
- Count House Segments After Sequential DestructionByteDance · OAByteDance interview questions