(Hacker Rank) Taum and B'day - Solution

Link to the problem: https://www.hackerrank.com/challenges/taum-and-bday

If it is cheaper to buy a gift and change its color instead of buying the appropriate color of the gift, we buy the necessary quantity of gifts and change their color. This solution is called Greedy, and it was used by the solution below.


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            long b = in.nextLong();
            long w = in.nextLong();
            long x = in.nextLong();
            long y = in.nextLong();
            long z = in.nextLong();
           
            long costB = 0;
            long costW = 0;
            if (x+z < y) {
                costW = (x+z)*w;
            }
            else {
                costW = y*w;
            }
           
            if (y+z < x) {
                costB = (y+z)*b;
            }
            else {
                costB = x*b;
            }
           
            System.out.println(costB+costW);
        }
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução