어려움 0명 해결 정답률 0%

Maximum Project Rewards

Choose nonoverlapping projects to earn the largest total reward.

배우게 될 내용

  • Sort intervals by end time
  • Find the last compatible project with binary search
  • Choose between taking and skipping a project

문제

번역본 보기

Each project is [start_day, end_day, reward]. It occupies every day from start_day through end_day, including both endpoints. You can participate in only one project on a day. Choose a subset with the largest total reward. A project starting on another project’s end day conflicts with it.

Implement project_rewards(projects).

Input

projects: Rows of three integers: start day, end day, and reward.

Output

Return the maximum total reward; the chosen projects need not be returned.

제약 조건

1 ≤ projects ≤ 200,000. 1 ≤ start_day ≤ end_day ≤ 1,000,000,000. Rewards are positive integers at most 1,000,000,000.

예시

예시 1
입력:
projects = [
    [1, 2, 4],
    [3, 4, 5],
    [2, 3, 10]
]

예상 출력:
expected = 10
입력:
std::vector<std::vector<long long>> projects = {
    {1, 2, 4},
    {3, 4, 5},
    {2, 3, 10}
};

예상 출력:
long long expected = 10;
입력:
long[][] projects = new long[][] {
    new long[] {1, 2, 4},
    new long[] {3, 4, 5},
    new long[] {2, 3, 10}
};

예상 출력:
long expected = 10;

설명

The first two projects earn 9 together. The last earns 10 and conflicts with both, so choose it alone.

예시 2
입력:
projects = [
    [1, 2, 4],
    [2, 3, 5]
]

예상 출력:
expected = 5
입력:
std::vector<std::vector<long long>> projects = {
    {1, 2, 4},
    {2, 3, 5}
};

예상 출력:
long long expected = 5;
입력:
long[][] projects = new long[][] {
    new long[] {1, 2, 4},
    new long[] {2, 3, 5}
};

예상 출력:
long expected = 5;

설명

Both occupy day 2. Choose only the second, with reward 5.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

This is a weighted interval scheduling problem with overlapping time ranges.

힌트 2

Sort projects by end day and combine each project with the latest compatible earlier one.

힌트 3

Be precise about conflicts: another project starting on an end day still overlaps.

예상 복잡도

O(n log n) time, O(n) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.