[LeetCode] 2542. Maximum Subsequence Score, Medium

2025. 4. 29. 16:55·CodingTest/LeetCode
반응형

1. 문제

  • 정수 배열 nums1과 nums2가 주어질 때, k개의 인덱스를 선택해서 nums1의 합 * nums2의 최소값이 최대가 되는 값을 구하라.

2. 해결

class MinHeap {
    private heap: number[];

    constructor() {
        this.heap = [];
    }

    getLeftChildIndex = (parentIndex) => parentIndex * 2 + 1;
    getRightChildIndex = (parentIndex) => parentIndex * 2 + 2;
    getParentIndex = (childIndex) => Math.floor((childIndex - 1) /2);

    swap = (i: number, j: number) => {
        [this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
    };

    peek = () => this.heap[0];
    size = () => this.heap.length;

    push(value: number) {
        this.heap.push(value);
        this.heapifyUp();
    }

    heapifyUp() {
        let index = this.heap.length - 1; // 마지막 요소.

        while(index > 0 && this.heap[this.getParentIndex(index)] > this.heap[index]) { // 부모 노드보다 자식 노드가 작은 경우.
            this.swap(index, this.getParentIndex(index)); // 스왑.
            index = this.getParentIndex(index);
        }
    }

    pop(): number | undefined {
        if(this.heap.length === 0) return undefined;
        if(this.heap.length === 1) return this.heap.pop();

        const top = this.heap[0];
        this.heap[0] = this.heap.pop(); // 마지막 값을 root로 놓고.
        this.heapifyDown();

        return top;
    }

    heapifyDown() {
        let index = 0;
        
        while(this.getLeftChildIndex(index) < this.heap.length) { // 자식이 있는 경우 반복, left가 없으면 right도 없기 때문에.
            // 왼쪽, 오른쪽 중 더 작은 자식을 찾음. 최소 힙이므로.
            let smallerChildIndex = this.getLeftChildIndex(index);
            const rightChildIndex = this.getRightChildIndex(index);

            if(rightChildIndex < this.heap.length && this.heap[rightChildIndex] < this.heap[smallerChildIndex]) {
                smallerChildIndex = rightChildIndex;
            }

            if(this.heap[index] <= this.heap[smallerChildIndex]) break; // 현재 노드가 자식보다 작거나 같으면 종료.

            this.swap(index, smallerChildIndex);
            index = smallerChildIndex;
        }
    }
}

function maxScore(nums1: number[], nums2: number[], k: number): number {
    const n = nums1.length;
    const pairs = nums1.map((num, i) => [nums2[i], num]);

    // nums2 기준 내림차순 정렬
    pairs.sort((a, b) => b[0] - a[0]);

    const heap = new MinHeap();
    let sum = 0;
    let max = Number.MIN_SAFE_INTEGER;

    for(let i = 0; i< pairs.length; i++) {
        const [num2, num1] = pairs[i];

        heap.push(num1);
        sum += num1;

        if(heap.size() > k) {
            sum -= heap.pop();
        }

        if(heap.size() === k) {
            max = Math.max(max, num2 * sum);
        }

    }
   
    return max;
}
  • 못 풀었다.
  • 원리는 nums2는 최솟값을 골라야 하는데, 곱하기 때문에 최솟값 중 최대 값을 골라야 한다.
  • nums2, nums1의 값들을 맵핑해서 관리하고, nums2를 내림차순 정렬하여 순서대로 탐색한다.
  • 탐색하면서 nums1의 값은 최소 힙에 큰 값을 k개만 유지하도록 한다. 
  • k개가 담겼을 때 최대값 갱신.
저작자표시 (새창열림)
'CodingTest/LeetCode' 카테고리의 다른 글
  • [LeetCode] 374. Guess Number Higher or Lower, Easy
  • [LeetCode] 2462. Total Cost to Hire K Workers, Medium
  • [LeetCode] 2336. Smallest Number in Infinite Set, Medium
  • [LeetCode] 215. Kth Largest Element in an Array, Medium
뜸부깅
뜸부깅
코딩에 대한 여러 개인적인 생각을 정리하고 공부를 하는 공간입니다!!
  • 뜸부깅
    코오오딩
    뜸부깅
  • 전체
    오늘
    어제
    • Note (429)
      • Skill (31)
        • Java & Spring (9)
        • Javascript & HTML & CSS (0)
        • React (0)
        • Next.js (22)
      • CodingTest (389)
        • 백준 온라인 저지(BOJ) (140)
        • 프로그래머스(Programmers) (79)
        • LeetCode (170)
      • Algorithm & Data Structure (6)
      • [Project] 포트폴리오 (3)
        • Front end (3)
        • Back end (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    백준2751
    component-scan
    TypeScript
    프로그래머스
    백준
    boj1427
    meidum
    백준1260
    알고리즘
    BOJ
    boj2108
    Easy
    Java
    leetcode 2236
    자바
    백준7576
    백준7576자바
    백준1427
    medium
    next 14
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
뜸부깅
[LeetCode] 2542. Maximum Subsequence Score, Medium
상단으로

티스토리툴바