[LeetCode] 2016. Maximum Difference Between Increasing Elements

Chobby·3일 전
1

LeetCode

목록 보기
651/680

😎풀이

  1. nums 2중 순회
    1-1. nums[i]nums[j] 이상인 경우 문제 요구에 따라 차이를 계산하지 않는다.
    1-2. 두 값의 차이를 계산해 최대 차잇값을 갱신
  2. 최대 차잇값 반환. 최대 차잇값 반환
function maximumDifference(nums: number[]): number {
    let maxDiff = -1
    for(let i = 0; i < nums.length - 1; i++) {
        for(let j = i + 1; j < nums.length; j++) {
            if(nums[i] >= nums[j]) continue
            maxDiff = Math.max(maxDiff, nums[j] - nums[i])
        }
    }
    return maxDiff
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글