PracticeSearching
Final Scoreboard Match
Context
In the arcade game Neon Rally, the scoreboard stores player scores in nondecreasing order. A repeated score can appear several times because players may tie. The board system needs the position of the last entry having a requested score so it can highlight the final matching record.
Problem
Write a function that searches a nondecreasing array of scoreboard scores for a target score. Return the greatest zero-based index whose value equals the target. If the target does not occur, return -1. Use the ordering of the scoreboard to search efficiently rather than checking every score in sequence.
Examples
Example 1
Input:
scores = [10, 20, 20, 20, 35, 50]target = 20Output:
3Explanation: For scores [10, 20, 20, 20, 35, 50] and target 20, the executed result is 3; the final recorded best is 3.
Example 2
Input:
scores = [5, 12, 18, 27, 34]target = 19Output:
-1Explanation: For scores [5, 12, 18, 27, 34] and target 19, the executed result is -1; the final recorded best is -1.
Example 3
Input:
scores = [7]target = 7Output:
0Explanation: For scores [7] and target 7, the executed result is 0; the final recorded best is 0.
Constraints
0 <= len(scores) <= 1000000 <= scores[i] <= 1000000 for every iscores: sorted non decreasing0 <= target <= 1000000Indices are zero-based.- Types:
scoresis int[],targetis int; result is int
Function signature
def find_last_score_index(scores: list[int], target: int) -> int
scoreslist[int]- A nondecreasing array of scores on the scoreboard.
targetint- The score whose final occurrence should be located.
- returns int
- The greatest zero-based index containing target, or -1 if no score matches.
Adapted from MBPP problem task_316 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Code
Saved