PracticeSearching

Final Scoreboard Match

Easy/MediumSearchingbinary searchstate trackingcomparison Time not estimated Not started

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 = 20
Output: 3
Explanation: 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 = 19
Output: -1
Explanation: 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 = 7
Output: 0
Explanation: For scores [7] and target 7, the executed result is 0; the final recorded best is 0.

Constraints

  • 0 <= len(scores) <= 100000
  • 0 <= scores[i] <= 1000000 for every i
  • scores: sorted non decreasing
  • 0 <= target <= 1000000
  • Indices are zero-based.
  • Types: scores is int[], target is int; result is int

Function signature

def find_last_score_index(scores: list[int], target: int) -> int
scores list[int]
A nondecreasing array of scores on the scoreboard.
target int
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.

Approved · c012r1-abs_770_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1