JAVA QUESTION AND ANSWER CLEARLY EXPLAINED
ok lets do this!\nfirst understand the question correctly:\nbasically we are the lemonade shop owner and the customer pays us either a $5 or $10 or $20 to buy a
ok lets do this!\nfirst understand the question correctly:\nbasically we are the lemonade shop owner and the customer pays us either a $5 or $10 or $20 to buy a lemonade whose cost is constant- $5 \n\n**This is a greedy problem because at each step we are greedily giving the change back to the customer in such a manner that we have maximum number of $5 bills with us!**\n\nbecause consider the situation when the customer gives us $20 and we have two $10 bills!\nin such a case we are unable to give back the change to the customer!\nOn the otherhand if we had four $5 bills or one $10 and two $5 bills then we can give back the change to the customer!\n\nSo our aim is to maximise the number if $5 bills!\n\nsee the code below \ncomments are added to clearly explain the conditions!\n\n```\nclass Solution {\n     public boolean lemonadeChange(int[] bills) {\n        int five = 0, ten = 0;\n        for (int i : bills) {\n            if (i == 5)  //if customer gives $5 then we simply take it as lemonade costs $5 \n                five++;\n            else if (i == 10) { //if we have $10 then we reduce number of $5 bills by one and increase $10 by one\n                five--; ten++;\n            }\n            else if (ten > 0 && five>=1) { //if we get $20 then we first try to give back $10+$5\n                ten--; five--;\n            }\n            else        // if we cannot give back $10+$5 then unfortunately we have to give back $5+$5+$5\n                five -= 3;\n            if (five < 0)  // if at any time we do not have any $5 left then we return false\n                return false;\n        }\n        return true;\n    }\n        \n}        \n```\n\nHOPE IT HELPS!\n\n        \n\n
['Java']