Created
February 2, 2018 21:55
-
-
Save caglarorhan/e1ba5488b9ec9fd0805dec071666cc52 to your computer and use it in GitHub Desktop.
496. Next Greater Element I -From leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} findNums | |
* @param {number[]} nums | |
* @return {number[]} | |
*/ | |
var nextGreaterElement = function(findNums, nums) { | |
const fNLength = findNums.length; | |
const nLength = nums.length; | |
let outPut = []; | |
findNums.forEach(function(i,index){ | |
const numIndex = nums.indexOf(i); | |
let foundAGreater=false; | |
for(var it=numIndex+1; it<=nLength; it++){ | |
if(i<nums[it]){ outPut.push(nums[it]); foundAGreater=true; break;} | |
} | |
if(!foundAGreater){outPut.push(-1)} | |
}); | |
return outPut; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
if subset from index = 1 begin, the position would begin index =1 at the parent set yet,which make output[1] = 3
so,your code var it=numIndex+1,should change to var it=numIndex