Lemonade Change solution in JS - Easy enough for Humans to understand rather than just machines
Intuition\nTook more of a human approach for this solution. Didn\'t feel like writing a shorter code,\n\n>\u201CAny fool can write code that a computer can unde
# Intuition\nTook more of a human approach for this solution. Didn\'t feel like writing a shorter code,\n\n>\u201CAny fool can write code that a computer can understand. Good programmers write code that humans can understand.\u201D \n> \n> ~ Martin Fowler, 2008.\n\n# Approach\nImagine having an actual lemonade stand. You take the bil from your customer, if the given bill is more than the price of the lemonade purchased then you return change, otherwise not.\n\nThe given code bellow is absolutely self explanatory as I promised a *Human Readable Code*. \n\n# Complexity\n- Time complexity: *O(n)*, where `n` is the length of the input array `bills`. The reason is the loop in the `lemonadeChange` function, which iterates through each bill in the array once.\n\n- Space complexity: *O(n)*, The `bills` Map in the Cashier class can grow in proportion to the number of `bills` processed in the `lemonadeChange` function.\n\n# Code\n```\n/**\n * Cashier class to handle bill transactions.\n */\nclass Cashier {\n    /**\n     * Constructor for the Cashier class.\n     */\n    constructor() {\n        this.bills = new Map();\n        this.PRICE_OF_LEMONADE = 5;\n        this.valid_bills = [20, 10, 5];\n    }\n\n    /**\n     * Check the balance of a specific bill.\n     * @param {number} bill - The denomination of the bill.\n     * @return {number} The number of bills of this denomination.\n     */\n    checkBalance(bill) {\n        return this.bills.get(bill) || 0;\n    }\n\n    /**\n     * Deposit a specific bill.\n     * @param {number} bill - The denomination of the bill.\n     * @param {number} [n=1] - The number of bills to deposit.\n     */\n    deposit(bill, n = 1) {\n        var _previousBalance = this.checkBalance(bill);\n\n        this.bills.set(bill, _previousBalance + n);\n    }\n\n    /**\n     * Withdraw a specific bill.\n     * @param {number} bill - The denomination of the bill.\n     * @param {number} [n=1] - The number of bills to withdraw.\n     */\n    withdraw(bill, n = 1) {\n        var _previousBalance = this.checkBalance(bill);\n\n        this.bills.set(bill, _previousBalance - n);\n    }\n\n    /**\n     * Return change for a given amount.\n     * @param {number} amount - The amount to return change for.\n     * @return {boolean} Whether the change can be returned or not.\n     */\n    return_change(amount) {\n        var _change = amount,\n            isReturnable = true;\n\n        for (var i = 0; i < this.valid_bills.length; i++) {\n            var _currentDenomination = this.valid_bills[i];\n\n            while (\n                _change >= _currentDenomination &&\n                this.checkBalance(_currentDenomination)\n            ) {\n                this.withdraw(_currentDenomination);\n                _change -= _currentDenomination;\n            }\n        }\n\n        if (_change !== 0) {\n            isReturnable = false;\n        }\n\n        return isReturnable;\n    }\n}\n\n/**\n * @param {number[]} bills\n * @return {boolean}\n */\nvar lemonadeChange = function (bills) {\n    // Empty bills array should not be processed\n    if (bills.length === 0) return false;\n\n    var cashier = new Cashier();\n\n    for (var i = 0; i < bills.length; i++) {\n        var _currentBill = bills[i];\n\n        // whatever the bill is, we put it in the cash box\n        // and then check whether change needs to be returned\n        cashier.deposit(_currentBill);\n\n        // we only have to return change if given bill is-\n        // more than the price of a lemonade\n        if (_currentBill > cashier.PRICE_OF_LEMONADE) {\n            var _change = _currentBill - cashier.PRICE_OF_LEMONADE;\n\n            if (!cashier.return_change(_change)) {\n                // since cannot return change for given bill,\n                // we remove the bill that we deposited earlier\n                cashier.withdraw(_currentBill);\n\n                return false;\n            }\n        }\n    }\n\n    return true;\n};\n```
['JavaScript']