- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose Amal and Bimal are playing a game. They have an array nums which determines n bricks with numbered on top of it. In this game, players can alternatively remove one, two or three bricks from the top, and the numbers marked on the removed bricks are added to the score of that player. If always Amal starts first, we have to find how much score Amal get secure at maximum.

So, if the input is like nums = [1,2,3,4,5], then the output will be 6 because, Amal can remove brick {1}, {1,2} or {1,2,3}, if Amal selects first two or three elements, then Bimal can take all and get maximum score, but if Amal selects 1 at first, Bimal can take at most {2,3,4} = 9 and Amal can take 5, so total score of Amal is 1+5 = 6.

To solve this, we will follow these steps

- INF := 9999
- n := size of nums
- reverse the list nums
- temp := an array of size n and fill with 0
- total := an array of size n and fill with 0
- for each index i, and value val in nums, do
- total[i] := total[i-1] + val

- temp[0] := nums[0]
- temp[1] := temp[0]+nums[1]
- temp[2] := temp[1]+nums[2]
- for i in range 3 to n - 1, do
- a := nums[i]
- b := nums[i] + nums[i-1]
- c := nums[i] + nums[i-1] + nums[i-2]
- temp[i] := maximum of a, b, c

- return temp[n-1]

Let us see the following implementation to get better understanding −

INF = 99999 def solve(nums): n = len(nums) nums.reverse() temp = [0]*n total = [0]*n for i, val in enumerate(nums): total[i] = total[i-1] + val temp[0] = nums[0] temp[1] = temp[0]+nums[1] temp[2] = temp[1]+nums[2] for i in range(3, n): a = nums[i] b = nums[i] + nums[i-1] c = nums[i] + nums[i-1] + nums[i-2] temp[i] = max(a, b, c) return temp[n-1] nums = [1,2,3,4,5] print(solve(nums))

[1,2,3,4,5]

6

- Related Questions & Answers
- Program to find maximum score in stone game in Python
- Program to find winner of array removal game in Python
- Program to find maximum score we can get in jump game in Python
- Program to find winner of a set element removal game in Python
- Python program to find score and name of winner of minion game
- Program to find maximum score of a good subarray in Python
- Program to find maximum score from removing stones in Python
- Program to find maximum additive score by deleting numbers in Python
- Program to find maximum score from performing multiplication operations in Python
- Program to check person 1 can win the candy game by taking maximum score or not in Python
- Program to find winner of stone game in Python
- Program to find the maximum score from all possible valid paths in Python
- Program to find out the maximum points collectable in a game in Python
- Python program to find runner-up score
- Program to count maximum score from removing substrings in Python

Advertisements