- 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 we are playing a game of cards. We are given several cards arranged linearly with a number on each of them. The numbers on the cards are randomly distributed; and at the beginning and the end of the cards, two cards are inserted with the number 1 on them. Now, in the game, we have to collect the maximum points by picking up the given cards. The cards are represented in an array 'cards' where the elements in the array represent the number of cards[i]. When we pick up card i, we collect points cards[i - 1] * cards[i] * cards[i + 1]. When we pick up a card, cards[i - 1] and cards[i] become neighbors. So, from these given cards we find out the maximum points that we can collect.

So, if the input is like cards = [7, 5, 9, 10], then the output will be 1025

So in the game, we can pick up −

The card at index 1 and get 7 * 5 * 9 = 315 points.

The card at the new index 1 and get 7 * 9 * 10 = 630 points.

The card at index 1 and get 7 * 10 = 70 points.

The last card and get 10 points.

Total points = 315 + 630 + 70 + 10 = 1025

To solve this, we will follow these steps −

- Define a function search() . This will take x, y
- temp := 0
- for z in range x + 1 to y, do
- temp := maximum of (temp, search(x, z) + search(z, y) + cards[x] * cards[z] * cards[y])

- return temp

- insert values 1 and 1 at the beginning and the end of the list cards respectively
- return search(0, size of cards - 1)

Let us see the following implementation to get better understanding −

def solve(cards): def search(x, y): temp = 0 for z in range(x + 1, y): temp = max(temp, search(x, z) + search(z, y) + cards[x] * cards[z] * cards[y]) return temp cards = [1] + cards + [1] return search(0, len(cards) - 1) print(solve([7, 5, 9, 10]))

[7, 5, 9, 10]

1025

- Related Questions & Answers
- Program to Find Out the Maximum Points From Removals in Python
- Program to Find Out the Points Achievable in a Contest in Python
- Program to find out if we win in a game in Python
- Program to find maximum score in stone game in Python
- Program to find out the minimum moves in a snakes and ladders game in Python
- Program to find maximum score of brick removal game in Python
- Program to Find Out the Maximum Final Power of a List in Python
- Program to find out the cells containing maximum value in a matrix in Python
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Program to find maximum score we can get in jump game in Python
- Program to find out the sum of the maximum subarray after a operation in Python
- Program to find out the maximum value of a 'valid' array in Python
- Program to find out is a point is reachable from the current position through given points in Python
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Program to find winner of stone game in Python

Advertisements