Bus Corridor Bonus Trail

Maximum reward along bounded jumps between different labels

HardDynamic ProgrammingHeapMonotonic Queue

A city is testing a bus corridor with checkpoints numbered from 0 to n - 1 in travel order. Checkpoint i belongs to service zone zones[i] and awards rewards[i] bonus points when visited.

A test journey must begin at checkpoint 0 and finish at checkpoint n - 1. From a visited checkpoint i, the next visited checkpoint j must satisfy both conditions:

  • i < j <= i + max_jump.
  • zones[i] != zones[j].

Checkpoints between i and j are skipped and award no points. There is no restriction on revisiting a zone after visiting a different zone.

Return the maximum total reward of a valid journey, including the rewards at both endpoints. Return -1 if no valid journey exists. When there is only one checkpoint, the journey consists of that checkpoint alone.

Examples

Example 1

Input: zones = [1,1,2,1,3], rewards = [4,8,2,7,3], max_jump = 2
Output: 16

Visiting checkpoints 0, 2, 3, and 4 respects the jump limit and changes zones at every move. Its collected reward is greater than that of any other valid journey.

Example 2

Input: zones = [5,5,5], rewards = [10,20,30], max_jump = 3
Output: -1

Every checkpoint belongs to the same zone, so no first move is legal and the final checkpoint cannot be reached.

Example 3

Input: zones = [9], rewards = [12], max_jump = 1
Output: 12

The starting checkpoint is already the destination, so its reward is collected without making a move.

Constraints

  • 1 <= zones.length == rewards.length <= 100000
  • 0 <= zones[i] <= 1000000000
  • 0 <= rewards[i] <= 1000000000
  • 1 <= max_jump <= zones.length

The intended complexity is O(n log n) time and O(n) space. Reward totals can exceed 32-bit integer range; all possible totals are exact JavaScript integers. Large generated cases are sized to respect the platform's JSON-length limit.

Hints

Show hint 1

Let dp[i] be the best reward of a journey ending at i. Its predecessor must lie in a moving index window and have a different zone.

Show hint 2

For each zone, maintain only candidates that can still become its best eligible predecessor. A heap of the current zone leaders lets you exclude the destination's zone without scanning every candidate.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you reconstruct any maximum-reward journey while preserving the same asymptotic running time?
  • How would the data structures change if each checkpoint had its own maximum outgoing jump length?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem