[LeetCode] 1913. Maximum Product Difference Between Two Pairs

Chobby·2일 전
1

LeetCode

목록 보기
629/650

😎풀이

  1. nums를 오름차 순 정렬
  2. 가장 큰 두 수 확인
  3. 가장 작은 두 수 확인
  4. 가장 큰 수들의 곱과, 가장 작은 수들의 곱의 차는 가장 큰 차가 됨됨
function maxProductDifference(nums: number[]): number {
    const sorted = nums.toSorted((a, b) => a - b)
    const [w, x] = sorted.slice(-2)
    const [y, z] = sorted.slice(0, 2)
    return (w * x) - (y * z)
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글