Education + Jobs Hiring Website - 2025
0 like 0 dislike
51 views

2. Touring a Building (Dynamic Programming)

❓ Question Reiteration

You are on floor zero (0) of a building with n floors. Your initial energy is 0. You must find the minimum possible absolute difference between the total time spent using the lift and the total time spent using the stairs to reach any floor i \in [1, n]. Energy never drops below 0.

Lift: Gains e_1 energy per floor, takes t_1 time per floor.

Stairs: Loses e_2 energy per floor, takes \lceil c/E \rceil time per floor, where c is a constant and E is your current energy before climbing the stairs.

Function: getMinDifference(int n, int e1, int t1, int e2, int c)

Input: n, e_1, t_1, e_2, c.

Output: The minimum possible difference |\text{time}_{\text{lift}} - \text{time}_{\text{stairs}}|.

 Example Walkthrough

For n=5, e_1=2, t_1=3, e_2=4, c=5. The answer is 11.

The optimal strategy to achieve this minimum difference is:

Floor 0 \to 1 (Lift): E=2, T_{\text{lift}}=3, T_{\text{stairs}}=0.

Floor 1 \to 2 (Lift): E=4, T_{\text{lift}}=6, T_{\text{stairs}}=0.

Floor 2 \to 3 (Lift): E=6, T_{\text{lift}}=9, T_{\text{stairs}}=0.

Floor 3 \to 4 (Lift): E=8, T_{\text{lift}}=12, T_{\text{stairs}}=0.

Floor 4 \to 5 (Stairs): E=8. \Delta T_{\text{stairs}} = \lceil 5/8 \rceil = 1. New E = \max(0, 8-4) = 4. T_{\text{lift}}=12, T_{\text{stairs}}=1.

Difference: |12 - 1| = 11.

 

ago in Online Assessments by Expert (136,160 points) | 51 views

Please log in or register to answer this question.