Raid Asset Residency

Fewest loads for a request sequence with changing storage limits

HardGreedyHeapOffline Processing

An online game's replay engine knows the entire sequence of assets it will request. Its asset cache starts empty, and each cached asset occupies one slot.

You receive an array requests, where requests[i] is the asset requested at step i, an integer initial_capacity, and a list changes. Each entry [step, capacity] in changes sets the cache capacity immediately before that step. Changes are listed in strictly increasing step order. Between changes, the capacity stays unchanged.

At each step, perform these actions in order:

  1. Apply any capacity change. If too many assets are cached, evict assets until the cache fits.
  2. Serve the requested asset. If it is already cached, no load is needed. Otherwise, load it, evicting a cached asset first if the cache is full.

A newly loaded asset must be placed in the cache to serve its request. Assets can be evicted at no cost, but an asset can only be loaded when it is requested. Every load has the same cost, including loading an asset that was loaded and evicted earlier.

You may use knowledge of all future requests when choosing evictions. Return the minimum total number of loads needed to serve the entire sequence.

Examples

Example 1

Input: requests = [1,2,3,1,2], initial_capacity = 2, changes = []
Output: 4

The first two requests fill the cache. When asset 3 is requested, evicting asset 2 preserves asset 1 for its next request. Asset 2 must then be loaded again at the final step; evicting asset 1 earlier would cause an additional reload.

Example 2

Input: requests = [1,2,3,2,3,2], initial_capacity = 3, changes = [[3,1],[5,2]]
Output: 5

Immediately before step 3, the capacity falls from three slots to one. Keeping asset 2 serves that step without a load. The following request needs asset 3, and the later capacity increase provides room to retain both assets afterward.

Example 3

Input: requests = [], initial_capacity = 1, changes = []
Output: 0

There are no requests, so no asset ever needs to be loaded.

Constraints

  • 0 <= requests.length <= 100000
  • 0 <= requests[i] <= 1000000000
  • 1 <= initial_capacity <= 100000
  • 0 <= changes.length <= max(0, requests.length - 1)
  • Each changes entry is [step, capacity], with 1 <= step < requests.length and 1 <= capacity <= 100000
  • The step values in changes are strictly increasing

Use the farthest-next-request eviction rule both when capacity shrinks and when a missing asset needs space. An exchange argument justifies each eviction: keeping an asset needed sooner instead of one needed later cannot force an earlier extra load; any discrepancy can be exchanged or reconciled at the later request. Scheduled capacity changes do not invalidate the argument because the exchanged cache states have equal size. The references use lazy heap entries, taking O((n + m) log(n + 1)) time and O(n + m) space for n requests and m changes. A cached asset requested at the current step has next-request position equal to that step during a capacity reduction, so it is preserved whenever the positive capacity permits it. Empty input returns zero.

Hints

Show hint 1

Precompute the next request position for every occurrence. An asset that is never requested again can be treated as having its next request after the end of the sequence.

Show hint 2

When an eviction is necessary, compare the next request positions of the cached assets. Use a priority queue and discard outdated entries when they reach the top.

Follow-up questions

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

  • How would you implement the same eviction rule with an indexed heap so the heap contains at most one entry per cached asset?
  • Does evicting the asset with the farthest next request remain optimal if different assets have different loading costs? Give a counterexample or a proof.

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