Skip to content

Commit dc3ab53

Browse files
committedJan 15, 2025
Add solution #496
1 parent be87794 commit dc3ab53

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
485|[Max Consecutive Ones](./0485-max-consecutive-ones.js)|Easy|
203203
491|[Non-decreasing Subsequences](./0491-non-decreasing-subsequences.js)|Medium|
204204
492|[Construct the Rectangle](./0492-construct-the-rectangle.js)|Easy|
205+
496|[Next Greater Element I](./0496-next-greater-element-i.js)|Easy|
205206
500|[Keyboard Row](./0500-keyboard-row.js)|Easy|
206207
501|[Find Mode in Binary Search Tree](./0501-find-mode-in-binary-search-tree.js)|Easy|
207208
502|[IPO](./0502-ipo.js)|Hard|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 496. Next Greater Element I
3+
* https://leetcode.com/problems/next-greater-element-i/
4+
* Difficulty: Easy
5+
*
6+
* The next greater element of some element x in an array is the first greater element
7+
* that is to the right of x in the same array.
8+
*
9+
* You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is
10+
* a subset of nums2.
11+
*
12+
* For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and
13+
* determine the next greater element of nums2[j] in nums2. If there is no next greater
14+
* element, then the answer for this query is -1.
15+
*
16+
* Return an array ans of length nums1.length such that ans[i] is the next greater
17+
* element as described above.
18+
*/
19+
20+
/**
21+
* @param {number[]} nums1
22+
* @param {number[]} nums2
23+
* @return {number[]}
24+
*/
25+
var nextGreaterElement = function(nums1, nums2) {
26+
return nums1.map(n => {
27+
let index = nums2.indexOf(n);
28+
while (nums2[index] <= n) {
29+
index++;
30+
}
31+
return index >= nums2.length ? -1 : nums2[index];
32+
});
33+
};

0 commit comments

Comments
 (0)