難しい 0件解決済み 正答率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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。