[LeetCode] Next Greater Element I - JavaScript

이은빈 EUNBIN·2021년 4월 29일
0
post-thumbnail

👩🏻‍💻 문제

LeetCode #496 Next Greater Element I



👩🏻‍💻 풀이

var nextGreaterElement = function(nums1, nums2) {
    let result = [];
    
    for(let i = 0; i < nums1.length; i++) {
        let greaterNumbers = [];
        let numIndex = nums2.indexOf(nums1[i]);
        
        for(let j = numIndex; j < nums2.length; j++) {
            nums2[j] > nums1[i] ? greaterNumbers.push(nums2[j]) : null;
        }
        if(greaterNumbers.length) {
            result.push(greaterNumbers[0]);
        } else {
            result.push(-1);
        }
    }
    
    return result;
};
profile
Frontend Engineer & Value Creator

0개의 댓글