Skip to content

Commit 43bead8

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.6 MB (79.83%)
1 parent fa5319d commit 43bead8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>You are given an integer array <code>nums</code> where the largest integer is <strong>unique</strong>.</p>
2+
3+
<p>Determine whether the largest element in the array is <strong>at least twice</strong> as much as every other number in the array. If it is, return <em>the <strong>index</strong> of the largest element, or return </em><code>-1</code><em> otherwise</em>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [3,6,1,0]
10+
<strong>Output:</strong> 1
11+
<strong>Explanation:</strong> 6 is the largest integer.
12+
For every other number in the array x, 6 is at least twice as big as x.
13+
The index of value 6 is 1, so we return 1.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [1,2,3,4]
20+
<strong>Output:</strong> -1
21+
<strong>Explanation:</strong> 4 is less than twice the value of 3, so we return -1.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>2 &lt;= nums.length &lt;= 50</code></li>
29+
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
30+
<li>The largest element in <code>nums</code> is unique.</li>
31+
</ul>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def dominantIndex(self, nums: List[int]) -> int:
3+
largest = nlargest(2, nums)
4+
return nums.index(largest[0]) if largest[0] >= largest[1] * 2 else -1

0 commit comments

Comments
 (0)