समस्या
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.
उदाहरण
इनपुट:
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.
इनपुट:
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।