LeetCode-860 柠檬水找零

LeetCode-860 柠檬水找零

在柠檬水摊上,每一杯柠檬水的售价为 5 美元。

顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。

每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。

注意,一开始你手头没有任何零钱。

如果你能给每位顾客正确找零,返回 true ,否则返回 false 。
在这里插入图片描述

package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GiveChange {

	public static void main(String[] args) {
		int[] temp=new int[] {10,10};
		System.out.println(lemonadeChange(temp));	
	}
	public static boolean lemonadeChange(int[] bills) {
		Arrays.sort(bills);
		boolean target=true;
		int count1=0,count2=0,count3=0;
		if (bills[0]!=5) {
			target=false;
			return target;
		}
		for (int i = 0; i < bills.length; i++) {
			if (bills[i]==5) {
				++count1;
			}if (bills[i]==10) {
				if (count1<1) {
					target=false;
					return target;
				}else {
					++count2;
					count1--;
				}
			}if (bills[i]==20) {
				if (count1==0||(count1<3&&count2==0)) {
					target=false;
					return target;
				}else {
					if (count2>0) {
						count1--;
						count2--;
					}else {
						if (count1>=3) {
							count1-=3;
						}else {
							target=false;
							return target;
						}
					}
					
				}
			}
		}
		return target;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌兮~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值