Sort by

recency

|

667 Discussions

|

  • + 0 comments

    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
    }
    
  • + 0 comments

    This is my approach

    def howManyGames(p, d, m, s): 
        n_games=0 
    
        while(s>=p):
                s-=p 
                n_games+=1
                p=max(p-d,m)
                
        return n_games 
    

    `

  • + 0 comments

    Here is my Python solution!

    def howManyGames(p, d, m, s):
        games = 0
        while True:
            if p > s:
                return games
            s -= p
            p = p - d if p - d >= m else m
            games += 1
    
  • + 0 comments

    Solution From my side

    def howManyGames(p, d, m, s):
        results = []
        results.append(p)
        while(sum(results)<=s):
            if(p>m and (p - d)>m):
                p = p - d
                results.append(p)
            else:
                results.append(m)
        return len(results)-1
    
  • + 0 comments
    def howManyGames(p, d, m, s):
        # Return the number of games you can buy
         
        count = 0 
        i = p
        
        while s - i >= 0:
            count += 1
            s -= i
            i = max(m, i - d )
                       
        return count