Skip to content

Commit fae4fbc

Browse files
committed
feat: update solutions to leetcode problem: No.0496
1 parent 48a49ba commit fae4fbc

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

solution/0400-0499/0496.Next Greater Element I/README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,52 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
先对将 nums2 中的每一个元素,求出其下一个更大的元素。随后对于将这些答案放入哈希映射(HashMap)中,再遍历数组 nums1,并直接找出答案。对于 nums2,可以使用单调栈来解决这个问题。
44+
4345
<!-- tabs:start -->
4446

4547
### **Python3**
4648

4749
<!-- 这里可写当前语言的特殊实现逻辑 -->
4850

4951
```python
50-
52+
class Solution:
53+
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
54+
mapper = dict()
55+
stack = []
56+
for num in nums2:
57+
while stack and stack[-1] < num:
58+
mapper[stack.pop()] = num
59+
stack.append(num)
60+
res = []
61+
for num in nums1:
62+
res.append(mapper.get(num, -1))
63+
return res
5164
```
5265

5366
### **Java**
5467

5568
<!-- 这里可写当前语言的特殊实现逻辑 -->
5669

5770
```java
58-
71+
class Solution {
72+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
73+
Deque<Integer> stack = new ArrayDeque<>();
74+
Map<Integer, Integer> map = new HashMap<>();
75+
for (int num : nums2) {
76+
while (!stack.isEmpty() && stack.peek() < num) {
77+
map.put(stack.pop(), num);
78+
}
79+
stack.push(num);
80+
}
81+
int n = nums1.length;
82+
int[] res = new int[n];
83+
for (int i = 0; i < n; ++i) {
84+
res[i] = map.getOrDefault(nums1[i], -1);
85+
}
86+
return res;
87+
}
88+
}
5989
```
6090

6191
### **...**

solution/0400-0499/0496.Next Greater Element I/README_EN.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,41 @@ The Next Greater Number of a number <b>x</b> in <code>nums1</code> is the first
7373
### **Python3**
7474

7575
```python
76-
76+
class Solution:
77+
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
78+
mapper = dict()
79+
stack = []
80+
for num in nums2:
81+
while stack and stack[-1] < num:
82+
mapper[stack.pop()] = num
83+
stack.append(num)
84+
res = []
85+
for num in nums1:
86+
res.append(mapper.get(num, -1))
87+
return res
7788
```
7889

7990
### **Java**
8091

8192
```java
82-
93+
class Solution {
94+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
95+
Deque<Integer> stack = new ArrayDeque<>();
96+
Map<Integer, Integer> map = new HashMap<>();
97+
for (int num : nums2) {
98+
while (!stack.isEmpty() && stack.peek() < num) {
99+
map.put(stack.pop(), num);
100+
}
101+
stack.push(num);
102+
}
103+
int n = nums1.length;
104+
int[] res = new int[n];
105+
for (int i = 0; i < n; ++i) {
106+
res[i] = map.getOrDefault(nums1[i], -1);
107+
}
108+
return res;
109+
}
110+
}
83111
```
84112

85113
### **...**

solution/0400-0499/0496.Next Greater Element I/Solution.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ public int[] nextGreaterElement(int[] nums1, int[] nums2) {
33
Deque<Integer> stack = new ArrayDeque<>();
44
Map<Integer, Integer> map = new HashMap<>();
55
for (int num : nums2) {
6-
while (!stack.isEmpty() && num > stack.peek()) {
6+
while (!stack.isEmpty() && stack.peek() < num) {
77
map.put(stack.pop(), num);
88
}
99
stack.push(num);
1010
}
11-
int[] res = new int[nums1.length];
12-
for (int i = 0; i < nums1.length; ++i) {
11+
int n = nums1.length;
12+
int[] res = new int[n];
13+
for (int i = 0; i < n; ++i) {
1314
res[i] = map.getOrDefault(nums1[i], -1);
1415
}
1516
return res;
1617
}
17-
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
3+
mapper = dict()
4+
stack = []
5+
for num in nums2:
6+
while stack and stack[-1] < num:
7+
mapper[stack.pop()] = num
8+
stack.append(num)
9+
res = []
10+
for num in nums1:
11+
res.append(mapper.get(num, -1))
12+
return res

0 commit comments

Comments
 (0)