[LeetCode] 2974. Minimum Number Game

Chobby·2025년 12월 16일

LeetCode

목록 보기
833/873

😎풀이

  1. nums를 내림차 순 정렬
  2. 정렬된 수가 남은 동안 순회
    2-1. 가장 작은 수를 Alice가 선택
    2-2. 두번째로 작은 수를 Bob이 선택
    2-3. Bob의 수를 arr에 추가
    2-4. Alice의 수를 arr에 추가
  3. arr 반환
function numberGame(nums: number[]): number[] {
    const sorted = nums.toSorted((a, b) => b - a)
    const arr = []
    while(sorted.length) {
        const alice = sorted.pop()
        const bob = sorted.pop()
        arr.push(bob)
        arr.push(alice)
    }
    return arr
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글