Skip to content

Commit 881d46a

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.6 MB (12.04%)
1 parent 994ac89 commit 881d46a

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
2+
3+
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
4+
5+
<p>For each <code>0 &lt;= i &lt; nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
6+
7+
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
14+
<strong>Output:</strong> [-1,3,-1]
15+
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
16+
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
17+
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
18+
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
25+
<strong>Output:</strong> [3,-1]
26+
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
27+
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
28+
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= nums1.length &lt;= nums2.length &lt;= 1000</code></li>
36+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 10<sup>4</sup></code></li>
37+
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
38+
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
39+
</ul>
40+
41+
<p>&nbsp;</p>
42+
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach 2: Better Brute Force
2+
3+
# Time: O(m * n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
8+
hash_table = {num: i for i, num in enumerate(nums2)}
9+
10+
res = [0] * len(nums1)
11+
12+
for i, num in enumerate(nums1):
13+
j = hash_table[num] + 1
14+
while j < len(nums2):
15+
if nums2[j] > num:
16+
res[i] = nums2[j]
17+
break
18+
j += 1
19+
else:
20+
res[i] = -1
21+
22+
return res
23+

0 commit comments

Comments
 (0)