JavaScript Solution 1 O(N*M)


  • 0
    var nextGreaterElement = function(findNums, nums) {
        let numMap = new Map();
        if (nums.length <= 0) return [];
        for(let i = 0 ; i<nums.length-1; ++i) {
          for(let j=i+1; j<nums.length; ++j) {
            if (nums[i] < nums[j]) {
              numMap.set(nums[i], nums[j]);
              break;
            }
          }
          if (!numMap.has(nums[i])) {
            numMap.set(nums[i], -1);
          }
        }
        numMap.set(nums[nums.length-1], -1);
        return findNums.map(item => numMap.get(item));
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.