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

Uber | SDE-2 | OA | 2026

Question 2: Touring the Building

You are on floor 0 of a building and need to reach floor n. You can use either the lift or stairs, with different energy and time implications:

  • Lift: Gain e1 energy per floor, takes t1 time per floor.
  • Stairs: Lose e2 energy per floor, time taken per floor = ⌈c/E⌉ where E is current energy and c is a constant.

You can use the lift to reach some floor i (0≤i≤n) and then use stairs for the remaining floors. Your energy starts at 0 and can never go below 0.

Find the minimum possible absolute difference between the time spent on the stairs and in the lift.

Example:

  • Input: n=5,e1=2,t1=3,e2=4,c=5
  • Output: 11
  • Explanation: The optimal strategy is to use the lift to floor 4, then stairs to floor 5:
    • Floor 0 → 1 (lift): E=2,Tlift​=3,Tstairs​=0
    • Floor 1 → 2 (lift): E=4,Tlift​=6,Tstairs​=0
    • Floor 2 → 3 (lift): E=6,Tlift​=9,Tstairs​=0
    • Floor 3 → 4 (lift): E=8,Tlift​=12,Tstairs​=0
    • Floor 4 → 5 (stairs): E=4, Tlift​=12, Tstairs​=⌈5/8⌉=1
      The difference between lift time and stairs time is ∣12−1∣=11.

Constraints:

  • 1≤n,e1,t1,e2,c≤105

Approach: The core logic is to iterate through every possible floor i where you might switch from the lift to the stairs. First, verify if a switch at floor i is valid (energy at floor i is i⋅e1, and it must be sufficient to cover (n−i)⋅e2 to avoid dropping below zero). If valid, calculate the stair time for the remaining floors using the ceiling division formula. Track the minimum difference ∣Tlift​−Tstairs​∣ across all valid switch floors.

ago in Online Assessments by Expert (137,360 points)
edited ago by | 39 views

Please log in or register to answer this question.