Marc's Cakewalk

Sort by

recency

|

431 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch explanation here : https://youtu.be/RRJoL2vDmZM

    long marcsCakewalk(vector<int> calorie) {
        sort(calorie.begin(), calorie.end(), [](int l, int r){return l > r;});
        long ans = 0;
        for(int i = 0; i < calorie.size(); i++){
            ans += pow(2, i) * calorie[i];
        }
        return ans;
    }
    
  • + 0 comments

    Here is my single line Python solution :

    def marcsCakewalk(calorie):
        # Write your code here    
        return sum((2**i) * calorie[i] for i in range(len(calorie))) if calorie.sort(reverse=True) or True else 0
    
  • + 0 comments

    Here is my Python solution :

    def marcsCakewalk(calorie):
        # Write your code here    
        calorie.sort(reverse = True)
        dist = 0
        exponent = 1
        for i in range(len(calorie)):
            dist += exponent * calorie[i]
            exponent *= 2
        
        return dist
    
  • + 0 comments

    Easy Java Sol:

    public static long marcsCakewalk(List<Integer> calorie) {
    
            Collections.sort(calorie, Collections.reverseOrder());
            int n = calorie.size();
            long miniMiles = 0;
    
            for(int i=0; i<n ;i++){
                double expo = Math.pow(2,i) * calorie.get(i);
                miniMiles = miniMiles + (long)expo;
            }
    
            return miniMiles;
    
        }
    
  • + 0 comments

    Haskell

    module Main where
    
    import Data.List (sortBy)
    
    solve :: [Int] -> Int
    solve cs = go 1 cs' 0
      where
        cs' = sortBy (flip compare) cs
        go _ [] acc = acc
        go p (c : cs) acc = go (p * 2) cs (acc + p * c)
    
    main :: IO ()
    main = getLine >> getLine >>= print . solve . map read . words