Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Created February 2, 2018 21:55
Show Gist options
  • Save caglarorhan/e1ba5488b9ec9fd0805dec071666cc52 to your computer and use it in GitHub Desktop.
Save caglarorhan/e1ba5488b9ec9fd0805dec071666cc52 to your computer and use it in GitHub Desktop.
496. Next Greater Element I -From leetcode
/**
* @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;
};
@jielong39
Copy link

jielong39 commented Sep 2, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment