Skip to content

Commit 38c6280

Browse files
committed
0496-next-greater-element-i.md Moved JavaScript code ahead of C#.
1 parent 724f174 commit 38c6280

1 file changed

Lines changed: 44 additions & 44 deletions

File tree

problems/0496-next-greater-element-i.md

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,50 @@ public:
205205
};
206206
```
207207
208+
## JavaScript
209+
### Brute force solution
210+
```javascript
211+
var nextGreaterElement = function (nums1, nums2) {
212+
const results = Array(nums1.length).fill(-1)
213+
214+
nums1.forEach((num1, i) => {
215+
let found = false
216+
217+
for (const num2 of nums2) {
218+
if (found && num2 > num1) {
219+
results[i] = num2
220+
break
221+
}
222+
223+
if (num1 == num2) {
224+
found = true
225+
}
226+
}
227+
})
228+
229+
return results
230+
};
231+
```
232+
233+
### Monotonic stack solution
234+
```javascript
235+
var nextGreaterElement = function (nums1, nums2) {
236+
const numToGreater = {}
237+
const indexStack = []
238+
239+
nums2.forEach((num, i) => {
240+
while (indexStack.length > 0 && nums2[indexStack.at(-1)] < num) {
241+
const index = indexStack.pop()
242+
numToGreater[nums2[index]] = num
243+
}
244+
245+
indexStack.push(i)
246+
})
247+
248+
return nums1.map((num) => numToGreater[num] || -1)
249+
};
250+
```
251+
208252
## C#
209253
### Brute force solution
210254
```c#
@@ -260,50 +304,6 @@ public class Solution {
260304
}
261305
```
262306

263-
## JavaScript
264-
### Brute force solution
265-
```javascript
266-
var nextGreaterElement = function (nums1, nums2) {
267-
const results = Array(nums1.length).fill(-1)
268-
269-
nums1.forEach((num1, i) => {
270-
let found = false
271-
272-
for (const num2 of nums2) {
273-
if (found && num2 > num1) {
274-
results[i] = num2
275-
break
276-
}
277-
278-
if (num1 == num2) {
279-
found = true
280-
}
281-
}
282-
})
283-
284-
return results
285-
};
286-
```
287-
288-
### Monotonic stack solution
289-
```javascript
290-
var nextGreaterElement = function (nums1, nums2) {
291-
const numToGreater = {}
292-
const indexStack = []
293-
294-
nums2.forEach((num, i) => {
295-
while (indexStack.length > 0 && nums2[indexStack.at(-1)] < num) {
296-
const index = indexStack.pop()
297-
numToGreater[nums2[index]] = num
298-
}
299-
300-
indexStack.push(i)
301-
})
302-
303-
return nums1.map((num) => numToGreater[num] || -1)
304-
};
305-
```
306-
307307
## Go
308308
### Brute force solution
309309
```go

0 commit comments

Comments
 (0)