Maximum sum of points over K buildings of N floors with M jumps
Given K buildings with N floors and a person standing on the ground who can make at most M jumps. The person has to climb to the top and collect maximum points. Find the maximum number of points that can be collected if the person can either climb up the stairs to the next floor of the same building or he can jump to the next floor of any of the adjacent buildings.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: N = 5, M = 2, K = 3,
points = [ [4, 5, 1, 2, 10],
[9, 7, 3, 20, 16],
[ 6, 12, 13, 9, 8] ]
Explanation: Start from building 2. Collect the 9 points in the first floor.
Jump to building 3. Collect the 12 points from the second and 13 points from third floor. (Jump 1)
Jump back to building 2. Collect the 20 points from fourth floor and 16 points in fifth floor. (Jump 2)
Number of jumps made = 2
Number of points collected= 9+12+13+20+16 = 70 points
Input: N = 4, M = 1, K = 3,
points = [ [7, 5, 4, 10],
[8, 9, 20, 16],
[ 12, 13, 3, 8] ]
Approach: Given problem can be solved using dynamic programming:
- First we define the state of dp
- dp(i, j, k) state – the maximum number of points that can be collected if the person is at ith floor having made at most j jumps and ended up at kth building
- Then we will define the base cases
- If there is only one floor then the person cannot jump, put the number of points at that location.
- If there are 0 jumps allowed then can choose one of the three building and keep climbing up on the same building.
- so, person can only take points from prev floor of same building and add current points
- Then we will define the transition between states
- if we are on first building then we can:
- come from the first building, one floor down, no jumps
- come from the next building, one floor down, one jump added
- add points stored in building 1 at ith floor for both cases
- If we are on intermediate building then we can:
- come from the previous building, one floor down, one jump added
- come from the current building, one floor down, no jump
- can come from the next building, one floor down, one jump added
- will add points stored in intermediate building at ith floor for all 3 cases
- If we are on last building then we can:
- come from previous building, one floor down, one jump added
- come from last building, one floor down, no jump added
- will add points stored in last building at ith floor for both cases
- For the final answer, return the maximum of points collected over the top floor of all building after engaging in permissible number of jumps.
Below is the implementation of the above approach:
Time Complexity: O(N*M*K) which means O(floors*jumps*buildings)
Auxiliary Space: O(N*M*K) which means O(floors*jumps*buildings)