Sort by

recency

|

933 Discussions

|

  • + 0 comments

    Here is my simple c++ solution, you can watch the explanation here : https://youtu.be/T9sxEzAbp-M

    long taumBday(int b, int w, int bc, int wc, int z) {
        long bp = min(bc, wc + z);
        long wp = min(wc, bc + z);
        return bp * b + wp * w;
    }
    
  • + 0 comments

    Here is my one line Python solution! There are three candidates for the lowest cost, buying all of them at the normal price, buying all black presents and converting to white presents, or buying all white presents and converting to black presents. Then, we return the smallest of these prices.

    def taumBday(b, w, bc, wc, z):
        return min([(b + w) * bc + z * w, (b + w) * wc + z * b, b * bc + w * wc])
    
  • + 0 comments

    some one may get the same issue. * First Code's logic is correct but it fails for some test cases, becuase of total cost is computed on all intgers (b, bc, w, wc, z), not on the long data types.

    long taumBday(int b, int w, int bc, int wc, int z) {
        // Check if converting black to white or white to black is cheaper
        if (bc > wc + z) {
            return (b * (wc + z)) + (w * wc);
        } else if (wc > bc + z) {
            return (b * bc) + (w * (bc + z));
        } else {
            // If neither conversion is cheaper, buy gifts at original prices
            return (b * bc) + (w * wc);
        }
    }
    
    ***Correct Solution:** *
    long taumBday(int b, int w, int bc, int wc, int z) {
        // Check if converting black to white or white to black is cheaper
        if (bc > wc + z) {
            return (b * (wc + z)) + (w * wc);
        } else if (wc > bc + z) {
            return (b * bc) + (w * (bc + z));
        } else {
            // If neither conversion is cheaper, buy gifts at original prices
            return (b * bc) + (w * wc);
        }
    }
    
    // see the most optimized code in the below solution, written by @alban_tyrex
    
  • + 1 comment

    This is my code in C#

        public static long taumBday(int b, int w, int bc, int wc, int z)
        {
            long normalCost = (long)b * bc + (long)w * wc;
            long alternativeCost1 = (long)b * bc + (long)w * (bc + z);
            long alternativeCost2 = (long)w * wc + (long)b * (wc + z);
            return Math.Min(normalCost, Math.Min(alternativeCost1, alternativeCost2));
        }
    
  • + 0 comments

    Haskell:

    module Main where
    
    import Control.Monad (replicateM_)
    
    solve :: Int -> Int -> Int -> Int -> Int -> Int
    solve b w bc wc z
        | bc > wc + z = (b + w) * wc + b * z
        | wc > bc + z = (b + w) * bc + w * z
        | otherwise = b * bc + w * wc
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            [b, w] <- map read . words <$> getLine
            [bc, wc, z] <- map read . words <$> getLine
            print $ solve b w bc wc z