Sort by

recency

|

664 Discussions

|

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

    Haskell

    module Main where
    
    solve p d m s =
        let seqH = takeWhile (>= m) $ iterate (subtract d) p
            seqT = repeat m
            sums = scanl1 (+) $ seqH ++ seqT
         in length $ takeWhile (<= s) sums
    
    main :: IO ()
    main = do
        [p, d, m, s] <- fmap (map read . words) getLine
        print $ solve p d m s
    
  • + 0 comments
    def howManyGames(p, d, m, s):
        ans=0
        while s>=p:
            s-=p
            ans+=1
            p-=d
            if p<m:
                p=m
        return ans
    
  • + 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
    oreo=a=p
      q=0
      while True:
        if a<=m:
          a=m
        else:
          a-=d
        if a<=m:
          a=m
        if oreo==s:
          q+=1
          return q
          break 
        elif oreo>s:
          oreo-=a
          return q
          break
        elif oreo<s:
          q+=1
          oreo+=a