Sort by

recency

|

674 Discussions

|

  • + 0 comments

    public static int howManyGames(int p, int d, int m, int s) { // Return the number of games you can buy

        int n=0;        
    
        while(s-p>=0)
        {            
            n++;
            s-=p;            
            p-=d;
    
            if(p<=m)
            {
                p=m;
            }      
        }
    
        return n;
    }
    
  • + 0 comments
    def how_many_games(p, d, m, s):
        if s < p:
            return 0
        c = 0
        while s >= m:
            s -= (p - c*d) if ((p - c*d) >= m) else m
            c += 1 if s >= 0 else 0
        return c
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-halloween-sale-problem-solution.html

  • + 0 comments

    PYTHON If in dought pls ask

    def howManyGames(p, d, m, s):
        # Return the number of games you can buy
        games=0
        i=0
        while s>=0:
            cost=p-d*i
            if cost>m:
                s-=cost
            else:
                s-=m
            games+=1
            i+=1
        return games-1
    
  • + 1 comment

    Here are my approaches in c++, you can see the explanation here : https://youtu.be/VFVaLMRzhV4

    Approach 1 O(n)

    int howManyGames(int p, int d, int m, int s) {
        int result = 0;
        while( s >= p){
            s -= p;
            p = max(m, p-d);
            result++;
        }
        return result;
    }
    

    Approach 2 O(n) with recursion

    int howManyGames(int p, int d, int m, int s){
        if( s < p) return 0;
        return howManyGames( max(m, p-d), d, m, s - p) + 1;
    }
    

    Approach 3 O(1), incomplete solution (failure of a test case), feel free to comment to share a way to complete it.

    int howManyGames(int p, int d, int m, int s) {
        int n = 1 + (p - m) / d;
        int total = (p + p - (n - 1) * d) * n / 2;
        if(total <= s) {
            return n + (s - total) / m;
        }
        else 
            return 0;
            // this return need to be computed properly
    }