File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 202202485|[ Max Consecutive Ones] ( ./0485-max-consecutive-ones.js ) |Easy|
203203491|[ Non-decreasing Subsequences] ( ./0491-non-decreasing-subsequences.js ) |Medium|
204204492|[ Construct the Rectangle] ( ./0492-construct-the-rectangle.js ) |Easy|
205+ 496|[ Next Greater Element I] ( ./0496-next-greater-element-i.js ) |Easy|
205206500|[ Keyboard Row] ( ./0500-keyboard-row.js ) |Easy|
206207501|[ Find Mode in Binary Search Tree] ( ./0501-find-mode-in-binary-search-tree.js ) |Easy|
207208502|[ IPO] ( ./0502-ipo.js ) |Hard|
You can’t perform that action at this time.
0 commit comments