Skip to content

Commit 65a0f0c

Browse files
committed
feat: add solutions to lc problem: No.0496
No.0496.Next Greater Element I
1 parent 18fc77c commit 65a0f0c

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,64 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int {
178178
}
179179
```
180180

181+
### **TypeScript**
182+
183+
```ts
184+
function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
185+
const map = new Map<number, number>();
186+
const stack: number[] = [Infinity];
187+
for (const num of nums2) {
188+
while (num > stack[stack.length - 1]) {
189+
map.set(stack.pop(), num);
190+
}
191+
stack.push(num);
192+
}
193+
return nums1.map(num => map.get(num) || -1);
194+
}
195+
```
196+
197+
### **Rust**
198+
199+
```rust
200+
use std::collections::HashMap;
201+
202+
impl Solution {
203+
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
204+
let mut map = HashMap::new();
205+
let mut stack = Vec::new();
206+
for num in nums2 {
207+
while num > *stack.last().unwrap_or(&i32::MAX) {
208+
map.insert(stack.pop().unwrap(), num);
209+
}
210+
stack.push(num);
211+
}
212+
nums1
213+
.iter()
214+
.map(|n| *map.get(n).unwrap_or(&-1))
215+
.collect::<Vec<i32>>()
216+
}
217+
}
218+
```
219+
220+
```rust
221+
impl Solution {
222+
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
223+
nums1.iter().map(|target| {
224+
let mut res = -1;
225+
for num in nums2.iter().rev() {
226+
if num == target {
227+
break;
228+
}
229+
if num > target {
230+
res = *num;
231+
}
232+
}
233+
res
234+
}).collect::<Vec<i32>>()
235+
}
236+
}
237+
```
238+
181239
### **...**
182240

183241
```

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,64 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int {
154154
}
155155
```
156156

157+
### **TypeScript**
158+
159+
```ts
160+
function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
161+
const map = new Map<number, number>();
162+
const stack: number[] = [Infinity];
163+
for (const num of nums2) {
164+
while (num > stack[stack.length - 1]) {
165+
map.set(stack.pop(), num);
166+
}
167+
stack.push(num);
168+
}
169+
return nums1.map(num => map.get(num) || -1);
170+
}
171+
```
172+
173+
### **Rust**
174+
175+
```rust
176+
use std::collections::HashMap;
177+
178+
impl Solution {
179+
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
180+
let mut map = HashMap::new();
181+
let mut stack = Vec::new();
182+
for num in nums2 {
183+
while num > *stack.last().unwrap_or(&i32::MAX) {
184+
map.insert(stack.pop().unwrap(), num);
185+
}
186+
stack.push(num);
187+
}
188+
nums1
189+
.iter()
190+
.map(|n| *map.get(n).unwrap_or(&-1))
191+
.collect::<Vec<i32>>()
192+
}
193+
}
194+
```
195+
196+
```rust
197+
impl Solution {
198+
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
199+
nums1.iter().map(|target| {
200+
let mut res = -1;
201+
for num in nums2.iter().rev() {
202+
if num == target {
203+
break;
204+
}
205+
if num > target {
206+
res = *num;
207+
}
208+
}
209+
res
210+
}).collect::<Vec<i32>>()
211+
}
212+
}
213+
```
214+
157215
### **...**
158216

159217
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
5+
let mut map = HashMap::new();
6+
let mut stack = Vec::new();
7+
for num in nums2 {
8+
while num > *stack.last().unwrap_or(&i32::MAX) {
9+
map.insert(stack.pop().unwrap(), num);
10+
}
11+
stack.push(num);
12+
}
13+
nums1
14+
.iter()
15+
.map(|n| *map.get(n).unwrap_or(&-1))
16+
.collect::<Vec<i32>>()
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
2+
const map = new Map<number, number>();
3+
const stack: number[] = [Infinity];
4+
for (const num of nums2) {
5+
while (num > stack[stack.length - 1]) {
6+
map.set(stack.pop(), num);
7+
}
8+
stack.push(num);
9+
}
10+
return nums1.map(num => map.get(num) || -1);
11+
}

0 commit comments

Comments
 (0)