Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Predict the Winner problem solution

YASH PAL, 31 July 2024

In this Leetcode Predict the Winner problem solution You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2.

Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of 0. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0] or nums[nums.length – 1]) which reduces the size of the array by 1. The player adds the chosen number to their score. The game ends when there are no more elements in the array.

Return true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true. You may assume that both players are playing optimally.

Leetcode Predict the Winner problem solution

Problem solution in Python.

from functools import lru_cache
from typing import List

class Solution:
    def PredictTheWinner(self, nums: List[int]) -> bool:
        if not nums:
            return None
        
        @lru_cache(maxsize=None)
        def bestScoreAdvantage(lo, hi):
            if lo > hi:
                return 0
            
            # Return the best score advantage you can get from:
            # - Playing one valid move
            # - Plug the negation of the best score advantage of the opponent
            return max(
                nums[lo] - bestScoreAdvantage(lo+1, hi),
                nums[hi] - bestScoreAdvantage(lo, hi-1)
            )
            
        return bestScoreAdvantage(0, len(nums)-1) >= 0

Problem solution in Java.

class Solution {
    public boolean PredictTheWinner(int[] nums) {
        int sum = 0;
        for(int i : nums)
            sum+=i;
        int[][] dp = new int[nums.length][nums.length];
        for(int[] d : dp)
            Arrays.fill(d, -1);
        return (sum+1)/2<=getAns(0, nums.length-1, dp,nums);
    }
    
    public int getAns(int i , int j, int[][] dp, int[] nums){
        if(i>j)
            return 0;
        if(dp[i][j] != -1)
            return dp[i][j];
        return dp[i][j] = Math.max(
        Math.min(nums[i]+getAns( i+1,j-1, dp, nums),nums[i]+getAns( i+2,j, dp, nums)),
        Math.min(nums[j]+getAns( i+1,j-1, dp, nums),nums[j]+getAns( i,j-2, dp, nums))
        );
    }
}

Problem solution in C++.

class Solution {
public:
    bool PredictTheWinner(vector<int>& nums) {
        if (nums.size()<3) return true;
        return helper(0, nums.size()-1, 1, 0, nums);
    }
    bool helper(int left, int right, int turn, int val, vector<int>& nums){
        if (left==right) return (turn*nums[left]+val)>=0;
        if (turn==1) {
            return helper(left+1, right, -turn, val+turn*nums[left], nums)||helper(left, right-1, -turn, val+turn*nums[right],nums);
        }
        else{
           return helper(left+1, right, -turn, val+turn*nums[left], nums) && helper(left, right-1, -turn, val+turn*nums[right], nums); 
        }
    }
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes