Loading...
Loading...
A railway ticket is immutable: withSeat and withTrain return new tickets. Because old tickets never change, the app can keep every version in an array, and undo just goes back one version. A change made after an undo starts from the version you went back to. Undo on the first version does nothing. Print the final ticket and how many ticket objects were made in total.
SEAT s, TRAIN t or UNDO.<passenger> <train> <seat> for the current ticket, then Versions <count>.Example
Input
Kavya 12951 WL-12 4 SEAT B2-14 TRAIN 12953 UNDO SEAT S4-31
Output
Kavya 12951 S4-31 Versions 4
UNDO goes back to the B2-14 ticket on 12951; the new seat is set on that version.
Try the problem first. Open one hint at a time.
Keep Ticket[] history of size q + 1 and an index current. Every SEAT or TRAIN builds from history[current] and stores the result at current + 1.
UNDO only moves current back by one, if it is above 0. No ticket is changed or rebuilt.
Count every ticket you create, including the first one.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor