Sort by

recency

|

932 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

    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
    
  • + 0 comments
    def taumBday(b, w, bc, wc, z):
        # Write your code here
        res= (w*wc) + (b*bc)
        should_convert = z < abs(bc-wc)
        if should_convert:
            if bc-wc > 0:
                bc = wc+z
                return (w*wc) + (b*bc)
            if wc-bc > 0:
                wc = bc+z
                return (w*wc) + (b*bc)  
        return res