Skip to content
Discussion options

You must be logged in to vote

We need to simulate the process of providing change to customers based on the bills they use to pay. The key is to track the number of $5 and $10 bills you have, as these are needed to provide change for larger bills

Let's implement this solution in PHP: 860. Lemonade Change

<?php
function lemonadeChange($bills) {
    $five = 0;
    $ten = 0;

    foreach ($bills as $bill) {
        if ($bill == 5) {
            $five++;
        } elseif ($bill == 10) {
            if ($five == 0) {
                return false;
            }
            $five--;
            $ten++;
        } else { // $bill == 20
            if ($ten > 0 && $five > 0) {
                $ten--;
                $five--;
  …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Aug 15, 2024
Maintainer Author

@topugit
Comment options

topugit Aug 15, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants