Program to find maximum score of brick removal game in Python

Suppose Amal and Bimal are playing a game with an array nums representing n bricks with numbers on top. In this game, players can alternatively remove one, two, or three bricks from the top, and the numbers on the removed bricks are added to that player's score. If Amal always starts first, we need to find the maximum score Amal can secure.

So, if the input is like nums = [1,2,3,4,5], then the output will be 6. Here's why: Amal can remove brick {1}, {1,2} or {1,2,3}. If Amal selects the first two or three elements, then Bimal can take all remaining bricks and get the maximum score. But if Amal selects 1 first, Bimal can take at most {2,3,4} = 9, and Amal can take 5, so Amal's total score is 1+5 = 6.

Algorithm Steps

To solve this problem, we will follow these steps ?

  • n := size of nums
  • reverse the list nums
  • temp := an array of size n filled with 0
  • for each index i and value val in nums, calculate cumulative totals
  • temp[0] := nums[0]
  • temp[1] := temp[0] + nums[1]
  • temp[2] := temp[1] + nums[2]
  • for i in range 3 to n-1, calculate maximum possible score by taking 1, 2, or 3 bricks
  • return temp[n-1]

Example

Let us see the following implementation to get better understanding ?

def solve(nums):
    n = len(nums)
    nums.reverse()
    temp = [0] * n
    
    # Handle base cases
    temp[0] = nums[0]
    if n > 1:
        temp[1] = temp[0] + nums[1]
    if n > 2:
        temp[2] = temp[1] + nums[2]
    
    # Calculate maximum score for remaining positions
    for i in range(3, n):
        # Option 1: Take only current brick
        a = nums[i]
        # Option 2: Take current and previous brick
        b = nums[i] + nums[i-1]
        # Option 3: Take current and previous two bricks
        c = nums[i] + nums[i-1] + nums[i-2]
        temp[i] = max(a, b, c)
    
    return temp[n-1]

# Test the function
nums = [1, 2, 3, 4, 5]
result = solve(nums)
print(f"Maximum score Amal can secure: {result}")

The output of the above code is ?

Maximum score Amal can secure: 6

How It Works

The algorithm works by reversing the array and using dynamic programming. We calculate the maximum score possible from each position by considering three options: taking 1, 2, or 3 bricks. The temp array stores the maximum score achievable from each position to the end of the array.

Key Points

  • The array is reversed to process from the end, making the calculation easier
  • At each position, we consider taking 1, 2, or 3 bricks and choose the maximum
  • The base cases handle positions 0, 1, and 2 directly
  • The final answer is stored in temp[n-1] which represents the maximum score from the start

Conclusion

This dynamic programming solution efficiently calculates the maximum score Amal can secure by considering all possible moves at each step. The time complexity is O(n) and space complexity is O(n).

Updated on: 2026-03-26T18:13:25+05:30

303 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements