Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check person 1 can win the candy game by taking maximum score or not in Python
Suppose two players are playing a candy game where several candies are placed in a line. Person 1 is given a list of numbers called nums representing the point value of each candy. On each player's turn, they can pick 1, 2, or 3 candies from the front of the line, delete them from the list, and add their points to their score. The game ends when all candies are taken, and the player with the higher score wins.
We need to determine if person 1 can win this game by playing optimally.
Example Scenario
If the input is nums = [1, 1, 2, 3, 50], the output will be True. Person 1 can take 1 candy (value 1), then regardless of what the opponent takes (1, 2, or 3 candies), person 1 can eventually take the candy with value 50.
Algorithm Approach
We use dynamic programming with the following steps ?
n:= size of numstable:= an array with three 0s to store the maximum profit for the next 3 positions-
For i from n?1 to 0 (reverse iteration), do:
profit:= ?infinity (worst case scenario)sum_val:= 0 (cumulative sum of candies taken)-
For j from i to minimum of i+3 and n, do:
sum_val+= nums[j] (add current candy value)profit= max(profit, sum_val ? table[j?i]) (current gain minus opponent's best response)
Update table with [profit, table[0], table[1]]
Return true if table[0] > 0, otherwise false
Implementation
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
# Test the solution
ob = Solution()
nums = [1, 1, 2, 3, 50]
result = ob.solve(nums)
print(f"Can person 1 win? {result}")
Can person 1 win? True
How It Works
The algorithm works backwards from the end of the candy list. At each position, it calculates the maximum advantage (profit) the current player can achieve by considering all possible moves (taking 1, 2, or 3 candies). The table array maintains the best outcomes for the next few positions, allowing optimal decision-making.
Test with Different Examples
ob = Solution()
# Example 1: Person 1 can win
nums1 = [1, 1, 2, 3, 50]
print(f"nums = {nums1}, Can win? {ob.solve(nums1)}")
# Example 2: Even distribution
nums2 = [1, 2, 3, 4, 5, 6]
print(f"nums = {nums2}, Can win? {ob.solve(nums2)}")
# Example 3: Small array
nums3 = [1, 2]
print(f"nums = {nums3}, Can win? {ob.solve(nums3)}")
nums = [1, 1, 2, 3, 50], Can win? True nums = [1, 2, 3, 4, 5, 6], Can win? True nums = [1, 2], Can win? True
Conclusion
This dynamic programming solution efficiently determines if person 1 can win the candy game by working backwards and calculating optimal moves. The algorithm considers all possible moves (1-3 candies) and maintains the best advantage at each position.
