Marc's Cakewalk

Sort by

recency

|

429 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

    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
    
  • + 0 comments
    def marcsCakewalk(calorie):
        calorie = sorted(calorie,reverse=True)
        n = len(calorie)
        return sum([2**i * v  for i, v in zip(range(n),calorie) ])
    
  • + 0 comments
    def marcsCakewalk(calorie):
        min_calorie = sorted(calorie, reverse=True)
        calories = 0
        for i in range(len(min_calorie)):
            calories += math.pow(2, i) * min_calorie[i]
        return int(calories)