Loading
Loading
A bank's basement has a row of vaults numbered from 0. Vault 0 is unlocked; every other vault starts locked. Inside each vault is a set of keys, and each key opens exactly one vault. A key can be used any number of times.
vaults[i] lists the vault numbers whose keys are stored in vault i. Return true if an auditor starting with vault 0 can eventually open every vault, and false otherwise.
vaults = [[1,3],[3,0,1],[2],[0]]open {0}vault 0 holds keys 1 and 3open {0,1,3}vault 1 gives 3, 0, 1; vault 3 gives 0 — nothing newvault 2 lockedits only key is inside vault 2 itselffalseSign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 |
Vault 0 holds key 1, vault 1 holds key 2, vault 2 holds key 3.
Example 2
| 0 | 1 | 3 |
|---|
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n + k) time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
| 1 | 3 | 0 | 1 |
|---|
| 2 | 2 |
|---|
| 3 | 0 |
|---|
Only vault 2 holds the key to vault 2, so it can never be opened.
Example 3
| 0 |
|---|
There is only vault 0, and it starts open.