Last Robot Score
Problem statement
Robots compete by score. While at least two robots remain, take the two robots with the highest scores and make them compete. If their scores are equal, both robots are eliminated. Otherwise, the robot with the larger score remains with its score reduced by the smaller score.
Return the score of the last remaining robot, or 0 if no robots remain.
Function
lastRobotScore(scores: int[]) → intExamples
Example 1
scores = [10,4,7,3]return = 210 and 7 leave 3, then 4 and 3 leave 1, then 3 and 1 leave 2. The last remaining robot has score 2.
Example 2
scores = [8,5,3]return = 08 and 5 leave 3, then the two 3s cancel.
Constraints
- The competition always uses the two currently highest scores.