Maximize Valid Trains
Learn this problemProblem statement
You are a train dispatcher. Locomotive i has ID locomotiveIds[i] and pull capacity pullCapacityTons[i]. Railcar j has ID railcarIds[j], weight weightTons[j], and length lengthFt[j].
A valid train uses:
- Between one and three locomotives.
- At least one railcar.
- Railcars whose combined length is at most
15000feet. - Railcars whose combined weight is at most the sum of the selected locomotives' pull capacities.
Each locomotive and railcar may belong to at most one train. First maximize the number of valid trains. Among solutions with the same train count, maximize the combined length of all used railcars.
Return [maximumTrainCount, maximumTotalLengthFt]. All numeric inputs have at most three digits after the decimal point, and comparisons are exact to one thousandth.
Function
maximizeValidTrains(locomotiveIds: String[], pullCapacityTons: double[], railcarIds: String[], weightTons: double[], lengthFt: double[]) → double[]Examples
Example 1
locomotiveIds = ["L1","L2"]pullCapacityTons = [100.0,60.0]railcarIds = ["C1","C2","C3"]weightTons = [70.0,50.0,40.0]lengthFt = [5000.0,4000.0,3000.0]return = [2.0,9000.0]Use one locomotive for railcar C1 and the other for C2. This creates two trains with total length 9000 feet. Two is optimal because only two locomotives exist.
Example 2
locomotiveIds = ["A","B"]pullCapacityTons = [40.0,50.0]railcarIds = ["X","Y"]weightTons = [80.0,60.0]lengthFt = [7000.0,6000.0]return = [1.0,7000.0]No single locomotive can pull either railcar. Combining both locomotives can pull one railcar, and choosing X maximizes length among the one-train solutions.
Constraints
1 <= locomotiveIds.length <= 6.1 <= railcarIds.length <= 8.- Each ID array contains unique non-empty strings.
pullCapacityTons.lengthequalslocomotiveIds.length.weightTons.lengthandlengthFt.lengthequalrailcarIds.length.- Every capacity, weight, and length is positive, at most
10^6, and has at most three decimal places.