Skip to content

Commit 4b224e7

Browse files
committed
Sync LeetCode submission Runtime - 115 ms (24.26%), Memory - 34.6 MB (24.26%)
1 parent 1a224df commit 4b224e7

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<p>An element <code>x</code> of an integer array <code>arr</code> of length <code>m</code> is <strong>dominant</strong> if <strong>more than half</strong> the elements of <code>arr</code> have a value of <code>x</code>.</p>
2+
3+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> with one <strong>dominant</strong> element.</p>
4+
5+
<p>You can split <code>nums</code> at an index <code>i</code> into two arrays <code>nums[0, ..., i]</code> and <code>nums[i + 1, ..., n - 1]</code>, but the split is only <strong>valid</strong> if:</p>
6+
7+
<ul>
8+
<li><code>0 &lt;= i &lt; n - 1</code></li>
9+
<li><code>nums[0, ..., i]</code>, and <code>nums[i + 1, ..., n - 1]</code> have the same dominant element.</li>
10+
</ul>
11+
12+
<p>Here, <code>nums[i, ..., j]</code> denotes the subarray of <code>nums</code> starting at index <code>i</code> and ending at index <code>j</code>, both ends being inclusive. Particularly, if <code>j &lt; i</code> then <code>nums[i, ..., j]</code> denotes an empty subarray.</p>
13+
14+
<p>Return <em>the <strong>minimum</strong> index of a <strong>valid split</strong></em>. If no valid split exists, return <code>-1</code>.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> nums = [1,2,2,2]
21+
<strong>Output:</strong> 2
22+
<strong>Explanation:</strong> We can split the array at index 2 to obtain arrays [1,2,2] and [2].
23+
In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 &gt; 3.
24+
In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 &gt; 1.
25+
Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split.
26+
It can be shown that index 2 is the minimum index of a valid split. </pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [2,1,3,1,1,1,7,1,2,1]
32+
<strong>Output:</strong> 4
33+
<strong>Explanation:</strong> We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1].
34+
In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 &gt; 5.
35+
In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 &gt; 5.
36+
Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split.
37+
It can be shown that index 4 is the minimum index of a valid split.</pre>
38+
39+
<p><strong class="example">Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> nums = [3,3,3,3,7,2,2]
43+
<strong>Output:</strong> -1
44+
<strong>Explanation:</strong> It can be shown that there is no valid split.
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
52+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
53+
<li><code>nums</code> has exactly one dominant element.</li>
54+
</ul>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach 1: Hash Map
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
from collections import defaultdict
7+
8+
class Solution:
9+
def minimumIndex(self, nums: List[int]) -> int:
10+
first_map = defaultdict(int)
11+
second_map = defaultdict(int)
12+
n = len(nums)
13+
14+
for num in nums:
15+
second_map[num] += 1
16+
17+
for index in range(n):
18+
num = nums[index]
19+
second_map[num] -= 1
20+
first_map[num] += 1
21+
22+
if first_map[num] * 2 > index + 1 and second_map[num] * 2 > n - index - 1:
23+
return index
24+
25+
return -1
26+

0 commit comments

Comments
 (0)