Ctrl K

Chests, Keys and Coins

Hard
There are n chests numbered from 0 to n - 1. For chest i you know: - unlocked[i] is 1 if it is not locked and 0 if it is; - coins[i] is how many coins it holds; - keys_inside[i] lists the chests whose keys lie inside it; - chests_found[i] lists the chests you receive when you open chest i. You start with the chests listed in start. A chest you own that is unlocked (or that you hold a key to) can be opened, and you take its coins, keys and new chests. Each chest is opened at most once, and receiving a chest you already have changes nothing. Return the total number of coins you can collect.
Example 1
unlocked=[1, 0, 0], coins=[3, 10, 4], keys_inside=[[2], [], [1]], chests_found=[[1, 2], [], []], start=[0] → 17

Chest 0 is open: it gives 3 coins, chests 1 and 2 and a key to 2. Chest 2 holds 4 coins and a key to 1, and chest 1 holds 10 coins. 17 in total.

Example 2
unlocked=[0, 1], coins=[5, 6], keys_inside=[[], []], chests_found=[[], []], start=[0] → 0

Chest 0 is locked with no key, and the open chest 1 is not ours. Zero coins.

Constraints
  • 1 ≤ n ≤ 10⁵, all arrays have length n
  • unlocked[i] is 0 or 1
  • 1 ≤ coins[i] ≤ 1000
  • Numbers in keys_inside[i], chests_found[i] and start are between 0 and n - 1 and distinct within each list
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLetter Order From a Dictionary HardLongest Uphill Walk HardBall Into the Hole HardWhich Machine to Cure HardWhich Machine to Unplug Hard
loading Python…
Input
unlocked=[1, 0, 0], coins=[3, 10, 4], keys_inside=[[2], [], [1]], chests_found=[[1, 2], [], []], start=[0]
Expected
17

Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.