Skip to content

Commit 810f3d7

Browse files
committed
[LeetCode Sync] Runtime - 21 ms (52.22%), Memory - 32 MB (52.78%)
1 parent ae633d3 commit 810f3d7

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<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 &lt; i</code> and <code>complexity[j] &lt; 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 &lt; i</code> and <code>complexity[j] &lt; 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>&nbsp;</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] &lt; complexity[1]</code>.</li>
35+
<li>Unlock computer 2 with password of computer 1 since <code>complexity[1] &lt; 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] &lt; complexity[2]</code>.</li>
42+
<li>Unlock computer 1 with password of computer 0 since <code>complexity[0] &lt; 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>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>2 &lt;= complexity.length &lt;= 10<sup>5</sup></code></li>
65+
<li><code>1 &lt;= complexity[i] &lt;= 10<sup>9</sup></code></li>
66+
</ul>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def countPermutations(self, complexity: List[int]) -> int:
3+
result = 1
4+
for i in range(1, len(complexity)):
5+
if complexity[i] <= complexity[0]:
6+
return 0
7+
result = result * i % (10**9 + 7)
8+
return result

0 commit comments

Comments
 (0)