Google | Onsite | How Far Can We Reach using Rope and Bricks
Anonymous User
7642

Given a zero-inexed array H of height of buildings, number of bricks b and number of ropes r. You start your journey from buiding 0 and move to adjacent building either using rope or bricks. You have limited number of bricks and ropes.
While moving from ith building to (i+1)th building,

  • if next building's height is less than or equal to the current buiding's height, you do not need rope or bricks.
  • if next building's height is greater than current buiding's height, you can either use one rope or (h[i+1] - h[i]) bricks.

So, question is How far can you reach from 0th buiding if you use bricks and ropes optimally? return index of building till which you can move.

Example 1:

Input : H = [4,2,7,6,9,11,14,12,8], b = 5, r = 2
Output: 8
Explanation: use rope to move from index 1 to index 2. 
use 3 bricks to move from index 3 to index 4. 
use 2 bricks to move from index 4 to index 5. 
use rope to move from index 5 to index 6. 
so we can reach at the end of the array using 2 ropes and 5 bricks. 

Example 2:

Input : H = [4,2,7,6,9,11,14,12,8], b = 5, r = 1
Output: 5
Explanation: use rope to move from index 1 to index 2. 
use 3 bricks to move from index 3 to index 4. 
use 2 bricks to move from index 4 to index 5. 
so we can reach at index 5 using 1 ropes and 5 bricks. 
Comments (23)