Consider a game, in which you have two types of powers, A and B and there are 3 types of Areas X, Y and Z. Every second you have to switch between these areas, each area has specific properties by which your power A and power B increase or decrease. We need to keep choosing areas in such a way that our survival time is maximized. Survival time ends when any of the powers, A or B reaches less than 0.
Initial value of Power A = 20 Initial value of Power B = 8 Area X (3, 2) : If you step into Area X, A increases by 3, B increases by 2 Area Y (-5, -10) : If you step into Area Y, A decreases by 5, B decreases by 10 Area Z (-20, 5) : If you step into Area Z, A decreases by 20, B increases by 5 It is possible to choose any area in our first step. We can survive at max 5 unit of time by following these choice of areas : X -> Z -> X -> Y -> X
This problem can be solved using recursion, after each time unit we can go to any of the area but we will choose that area which ultimately leads to maximum survival time. As recursion can lead to solving same subproblem many time, we will memoize the result on basis of power A and B, if we reach to same pair of power A and B, we won’t solve it again instead we will take the previously calculated result.
Given below is the simple implementation of above approach.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Maximum value with the choice of either dividing or considering as it is
- Largest area rectangular sub-matrix with equal number of 1's and 0's
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Sum of cost of all paths to reach a given cell in a Matrix
- Ackermann's function using Dynamic programming
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Find distance between two nodes in the given Binary tree for Q queries
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Probability of getting all possible values on throwing N dices