860. Lemonade Change

860. Lemonade Change

class Solution {
		public static boolean lemonadeChange(int[] bills) {
		HashMap d = new HashMap();
		d.put(5, 0);
		d.put(10, 0);
		for (int i = 0; i < bills.length; i++) {
			if (bills[i] == 5) {
				d.put(5, d.get(5) + 1);
			} else if (bills[i] == 10) {
				if (d.get(5) <= 0)
					return false;
				d.put(5, d.get(5) - 1);
				d.put(10, d.get(10) + 1);
			} else if (bills[i] == 20) {
				if ((d.get(10) - 1) < 0) { // no 10's
					if (d.get(5) - 3 < 0)
						return false;
				} else {// there are 10's
					if (d.get(5) - 1 = 1 && d.get(5) >= 1) {
					d.put(5, d.get(5) - 1);
					d.put(10, d.get(10) - 1);
				} else if (d.get(5) - 3 >= 0) {
					d.put(5, d.get(5) - 3);
				}
			}
		}
		return true;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s