Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Guess Number Higher or Lower II problem solution

YASH PAL, 31 July 2024

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

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)

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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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