|
| 1 | +<h2><a href="https://leetcode.com/problems/count-the-number-of-computer-unlocking-permutations">3864. Count the Number of Computer Unlocking Permutations</a></h2><h3>Medium</h3><hr><p>You are given an array <code>complexity</code> of length <code>n</code>.</p> |
| 2 | + |
| 3 | +<p>There are <code>n</code> <strong>locked</strong> computers in a room with labels from 0 to <code>n - 1</code>, each with its own <strong>unique</strong> password. The password of the computer <code>i</code> has a complexity <code>complexity[i]</code>.</p> |
| 4 | + |
| 5 | +<p>The password for the computer labeled 0 is <strong>already</strong> decrypted and serves as the root. All other computers must be unlocked using it or another previously unlocked computer, following this information:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li>You can decrypt the password for the computer <code>i</code> using the password for computer <code>j</code>, where <code>j</code> is <strong>any</strong> integer less than <code>i</code> with a lower complexity. (i.e. <code>j < i</code> and <code>complexity[j] < complexity[i]</code>)</li> |
| 9 | + <li>To decrypt the password for computer <code>i</code>, you must have already unlocked a computer <code>j</code> such that <code>j < i</code> and <code>complexity[j] < complexity[i]</code>.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>Find the number of <span data-keyword="permutation-array">permutations</span> of <code>[0, 1, 2, ..., (n - 1)]</code> that represent a valid order in which the computers can be unlocked, starting from computer 0 as the only initially unlocked one.</p> |
| 13 | + |
| 14 | +<p>Since the answer may be large, return it <strong>modulo</strong> 10<sup>9</sup> + 7.</p> |
| 15 | + |
| 16 | +<p><strong>Note</strong> that the password for the computer <strong>with label</strong> 0 is decrypted, and <em>not</em> the computer with the first position in the permutation.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
| 20 | + |
| 21 | +<div class="example-block"> |
| 22 | +<p><strong>Input:</strong> <span class="example-io">complexity = [1,2,3]</span></p> |
| 23 | + |
| 24 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 25 | + |
| 26 | +<p><strong>Explanation:</strong></p> |
| 27 | + |
| 28 | +<p>The valid permutations are:</p> |
| 29 | + |
| 30 | +<ul> |
| 31 | + <li>[0, 1, 2] |
| 32 | + <ul> |
| 33 | + <li>Unlock computer 0 first with root password.</li> |
| 34 | + <li>Unlock computer 1 with password of computer 0 since <code>complexity[0] < complexity[1]</code>.</li> |
| 35 | + <li>Unlock computer 2 with password of computer 1 since <code>complexity[1] < complexity[2]</code>.</li> |
| 36 | + </ul> |
| 37 | + </li> |
| 38 | + <li>[0, 2, 1] |
| 39 | + <ul> |
| 40 | + <li>Unlock computer 0 first with root password.</li> |
| 41 | + <li>Unlock computer 2 with password of computer 0 since <code>complexity[0] < complexity[2]</code>.</li> |
| 42 | + <li>Unlock computer 1 with password of computer 0 since <code>complexity[0] < complexity[1]</code>.</li> |
| 43 | + </ul> |
| 44 | + </li> |
| 45 | +</ul> |
| 46 | +</div> |
| 47 | + |
| 48 | +<p><strong class="example">Example 2:</strong></p> |
| 49 | + |
| 50 | +<div class="example-block"> |
| 51 | +<p><strong>Input:</strong> <span class="example-io">complexity = [3,3,3,4,4,4]</span></p> |
| 52 | + |
| 53 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 54 | + |
| 55 | +<p><strong>Explanation:</strong></p> |
| 56 | + |
| 57 | +<p>There are no possible permutations which can unlock all computers.</p> |
| 58 | +</div> |
| 59 | + |
| 60 | +<p> </p> |
| 61 | +<p><strong>Constraints:</strong></p> |
| 62 | + |
| 63 | +<ul> |
| 64 | + <li><code>2 <= complexity.length <= 10<sup>5</sup></code></li> |
| 65 | + <li><code>1 <= complexity[i] <= 10<sup>9</sup></code></li> |
| 66 | +</ul> |
0 commit comments