Problem · Simulation
Resolve Battles
Learn this problemProblem statement
The input is a list of strings, each describing one army's action.
A Munich Holdmeans Army A stays in Munich.B Warsaw Support Ameans Army B stays in Warsaw and adds one strength to Army A.C Bohemia Move Munichmeans Army C moves toward Munich.
If a supporting army's starting location is attacked by any Move order, its support is canceled.
Practice rule
- All orders are resolved at the same time.
- A moving army finishes at its target location. A holding or supporting army stays at its starting location.
- At each destination, the unique strongest army survives and every weaker army is defeated.
- If two or more armies tie for the highest strength, every army at that destination is defeated.
- Return one result per army in input order, formatted as
Army LocationorArmy [dead].
Function
resolveBattles(actions: List<String>) → List<String>Examples
Example 1
actions = ["A Munich Hold", "B Warsaw Support A", "C Bohemia Move Munich"]return = ["A Munich", "B Warsaw", "C [dead]"]In this scenario, Army C has a strength of 1, and Army A has a strength of 2 (due to support from Army B). As a result, Army C loses, and Army A successfully defends Munich. The final state of each army is as follows:
- Army A remains in Munich.
- Army B remains in Warsaw.
- Army C is defeated and is marked as [dead].