Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Guess Number Higher or Lower II problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Guess Number Higher or Lower II problem solution, We are playing the Guessing Game. The game will work as follows:

I pick a number between 1 and n.

  1. You guess a number.
  2. If you guess the right number, you win the game.
  3. If you guess the wrong number, then I will tell you whether the number I picked is higher or lower, and you will continue guessing.
  4. Every time you guess a wrong number x, you will pay x dollars. If you run out of money, you lose the game.

we have given a particular n, return the minimum amount of money you need to guarantee a win regardless of what number I pick.

Leetcode Guess Number Higher or Lower II problem solution

Leetcode Guess Number Higher or Lower II problem solution in Python.

class Solution:
    def getMoneyAmount(self, n: int) -> int:
        @lru_cache(maxsize=None)
        def rec(a, b):
            if b < 5:
                return [0, 0, 1, 2, 4][b]
            elif b == a+2:
                return a+1
            else:
                return min(i + max(rec(a,i-1),rec(i+1,b)) for i in range(b-3, a, -4))
        return rec(1, n)

Guess Number Higher or Lower II problem solution in Java.

class Solution {
    public int getMoneyAmount(int n) {
        if(n == 1)
            return 0;
        if(n == 2)
            return 1;
        int dp[][] = new int [n+2][n+2];
        for(int g=0;g<n;g++){
            for(int i=1,j=g+1;j<=n;j++,i++){
                if(g == 0)
                    dp[i][j] = 0;
                else if(g == 1)
                    dp[i][j] = Math.max(i,j);
                else{
                    int min = Integer.MAX_VALUE;
                    for(int k=i;k<j;k++){
                        min = Math.min(min,Math.max(dp[i][k-1]+k,dp[k+1][j]+k));
                    }
                    dp[i][j] = min;
                }
            }
        }
        return dp[1][n];
    }
}

Problem solution in C++.

class Solution {
public:
    vector<vector<int>> storedResults;
    
    int getMoneyAmount(int n) {
        storedResults.clear();
        storedResults.resize(n+2, vector<int>(n+2, -1));
        
        return calc(1, n);
    }
    
    int calc(int low, int high)
    {
        if (low >= high)
        {
            return 0;
        }
        
        int minCost = INT_MAX;
        
        for (int i = (low + high) / 2; i <= high; ++i)
        {
            int rightCost = storedResults[i+1][high];
            if (rightCost == -1)
            {
                rightCost = calc(i+1, high);
                storedResults[i+1][high] = rightCost;
            }
            int leftCost = storedResults[low][i-1];
            if (leftCost == -1)
            {
                leftCost = calc(low, i-1);
                storedResults[low][i-1] = leftCost;
            }
            
            int cost = i + max(rightCost, leftCost);
            minCost = min(cost, minCost);
        }
        
        return minCost;
    }
};

static auto x = [](){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return nullptr;
}();

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes