- 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 two players are playing a game. Where several candies placed on a line, and person 1 is given a list of numbers called nums that is representing the point value of each candy. On each person's turn, they can pick 1, 2, or 3 candies from the front of the line, then delete them from list, and get the sum of their points added to their score. This game will end when all the candies are deleted and the person with the higher score will be the winner. We have to check person 1 can win this game or not.

So, if the input is like nums = [1, 1, 2, 3, 50], then the output will be True, as person 1 can take 1 candy, then the other player has to take 1, 2 or 3 candies. In any case, person 1 can take the candy with the value of 50.

To solve this, we will follow these steps −

n := size of nums

table := an array with three 0s.

for i in range n − 1 to 0, decrease by 1, do

profit := −inf

sum_val := 0

for j in range i to minimum of i + 3 and n, do

sum_val := sum_val + nums[j]

profit := maximum of profit and (sum_val − table[j − i])

set table := a list with three values [profit, table[0], table[1]]

return true when table[0] > 0, otherwise false

Let us see the following implementation to get better understanding −

import math class Solution: def solve(self, nums): n = len(nums) table = [0, 0, 0] for i in range(n − 1, −1, −1): profit = −math.inf sum_val = 0 for j in range(i, min(i + 3, n)): sum_val += nums[j] profit = max(profit, sum_val − table[j − i]) table[:] = [profit, table[0], table[1]] return table[0] > 0 ob = Solution() nums = [1, 1, 2, 3, 50] print(ob.solve(nums))

[1, 1, 2, 3, 50]

True

- Related Questions & Answers
- Program to check whether first player win in candy remove game or not in Python?
- Program to check whether Amal can win stone game or not in Python
- Program to find maximum score in stone game in Python
- Program to find maximum score we can get in jump game in Python
- Program to find maximum score of brick removal game in Python
- Program to check person can reach top-left or bottomright cell avoiding fire or not in Python
- Program to check whether first player can win a game where players can form string char by char in C++
- Program to check we can cross river by stones or not in Python
- Program to find number of possible moves to start the game to win by the starter in Python
- Program to check first player can win by reaching total sum to target in Python
- Program to check robbers can rob the vault or not in Python
- Program to find maximum additive score by deleting numbers in Python
- Program to find maximum amount we can get by taking different items within the capacity in Python
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check we can reach at position n by jumping or not in Python

Advertisements