모든 챌린지
어려움 1명 해결 정답률 100.0%

Minimum Elevator Rides

Group people into as few weight-limited elevator rides as possible.

배우게 될 내용

  • Represent groups of people as bitmasks
  • Store the best rides and last ride weight per subset
  • Handle exponential search spaces cleverly

문제

번역본 보기

Each person must ride exactly once. Any group can share a ride if the sum of its weights is no greater than capacity. People may be regrouped freely; their input order does not matter. Minimize the number of rides.

Implement elevator_rides(weights, capacity).

Input

weights: The weight of each person.

capacity: The maximum combined weight in a ride.

Output

Return the minimum number of rides.

제약 조건

1 ≤ people ≤ 20. 1 ≤ capacity ≤ 1,000,000,000. Every weight is between 1 and capacity.

예시

예시 1
입력:
weights = [3, 4, 5]
capacity = 7

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

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

예상 출력:
long expected = 2;

설명

Send 3 + 4 together and 5 separately. Total weight 12 cannot fit in one ride of capacity 7.

예시 2
입력:
weights = [2, 2, 2]
capacity = 6

예상 출력:
expected = 1
입력:
std::vector<long long> weights = {2, 2, 2};
long long capacity = 6;

예상 출력:
long long expected = 1;
입력:
long[] weights = new long[] {2, 2, 2};
long capacity = 6;

예상 출력:
long expected = 1;

설명

All three people fit together because 2 + 2 + 2 = 6.

접근 방법

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

힌트 1

Since the number of people is small, consider subsets rather than individual rides.

힌트 2

Track both how many rides are used and how full the current last ride is for each subset.

힌트 3

When two states use the same number of rides, prefer the one with smaller current load.

예상 복잡도

O(2^n * n) time, O(2^n) extra space

참고 자료

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

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 831 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 솔루션

스포일러: 아래에 1개 전체 솔루션 있음 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.