Dynamic Programming - Elevator Rides CSES

53 Views Asked by At

Here is the problem:

There are n people who want to get to the top of a building which has only one elevator. You know the weight of each person and the maximum allowed weight in the elevator. What is the minimum number of elevator rides?

Constraints

1 <= n <= 20
1 <= x <= 10^9
1 <= w_i <= x

for solving this i applied 2 pointer approach but i am getting wrong answer I am not able to figure out the test case when the 2 pointer approach will fail I'm first sorting the weights and then for every heaviest weight i am incrementing answer by 1 and while doing so also clubbing as much lower weights with it till total_sum <= max_allowed_weight

1

There are 1 best solutions below

1
Usukhbayar Bayaraa On

How about you add heaviest person avaiable that can fit into the current ride till no more person can be added.

And repeat until no person available.

I think it is some kind of greedy and much simpler solution.

input:

5 10
4 4 4 4 4 2 2 2 2 2

calc:

1: 4 + 4 + 2
2: 4 + 4 + 2
3: 4 + 2 + 2 + 2

output:

3