You have a pool of locomotives and railcars. Partition any subset of them into valid trains while using each locomotive and railcar at most once.
A valid train:
-
Uses between one and three locomotives.
-
Uses at least one railcar.
-
Has total railcar length at most 15,000 feet.
-
Has total railcar weight at most the sum of its locomotives' pull capacities.
Optimize lexicographically:
-
Maximize the number of valid trains.
-
Among solutions with that train count, maximize the sum of railcar lengths across all trains.
Return only the two objective values, not an equipment allocation. This makes the answer unique even when several allocations attain the same optimum.
To make decimal comparisons exact across languages, all measurements are supplied as integers scaled by 100:
Locomotive {
id: string
pullCapacityCentiTons: integer
}
Railcar {
id: string
weightCentiTons: integer
lengthCentiFeet: integer
}
OptimizationResult {
trainCount: integer
totalLengthCentiFeet: integer
}
For example, 12,345 centi-feet means 123.45 feet. The per-train length limit is therefore 1,500,000 centi-feet.
Constraints
-
0 <= locomotives.length <= 10
-
0 <= railcars.length <= 18
-
IDs are unique within and across both arrays.
-
All capacities, weights, and lengths are positive and fit in signed 32-bit integers.
-
The combined length across trains can exceed a signed 32-bit integer; use 64-bit arithmetic for the returned total.
-
Equipment may remain unused.
Example 1
locomotives = [
{id: "L1", pullCapacityCentiTons: 10000},
{id: "L2", pullCapacityCentiTons: 8000}
]
railcars = [
{id: "C1", weightCentiTons: 6000, lengthCentiFeet: 700000},
{id: "C2", weightCentiTons: 4000, lengthCentiFeet: 600000},
{id: "C3", weightCentiTons: 8000, lengthCentiFeet: 900000}
]
result = {trainCount: 2, totalLengthCentiFeet: 2200000}
One optimal arrangement gives L1 the first two railcars and L2 the third. Both trains respect their weight and length limits, and all three railcars contribute 22,000 feet in total.
Example 2
locomotives = [
{id: "L1", pullCapacityCentiTons: 10000}
]
railcars = [
{id: "C1", weightCentiTons: 8000, lengthCentiFeet: 900000},
{id: "C2", weightCentiTons: 8000, lengthCentiFeet: 700000}
]
result = {trainCount: 1, totalLengthCentiFeet: 900000}
Only one train can be built because there is one locomotive. The two railcars cannot share it because their combined weight exceeds its capacity, so the longer railcar wins the secondary objective.