860. Lemonade Change #324
-
Topics: At a lemonade stand, each lemonade costs Note that you do not have any change in hand at first. Given an integer array Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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--;
} elseif ($five >= 3) {
$five -= 3;
} else {
return false;
}
}
}
return true;
}
// Example usage:
$bills = [5, 5, 5, 10, 20];
echo lemonadeChange($bills) ? 'true' : 'false'; // Output: true
$bills = [5, 5, 10, 10, 20];
echo lemonadeChange($bills) ? 'true' : 'false'; // Output: false
?> Explanation:
Edge Cases:
|
Beta Was this translation helpful? Give feedback.
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