860. Lemonade Change
Link: https://leetcode.com/problems/lemonade-change/
Description
At a lemonade stand, each lemonade costs $5
. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5
, $10
, or $20
bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5
.
Note that you do not have any change in hand at first.
Given an integer array bills
where bills[i]
is the bill the ith
customer pays, return true
if you can provide every customer with the correct change, or false
otherwise.
Approach
- Initialize
cnt5
to save the number of$5
bill,cnt10
to save the number of$10
bill. - Loop through
bills
:- If the bill equals to 5, add it to
cnt5
. - Else if the bill equals to 10. if there remains
$5
bill (cnt5 > 0
), delete 1 bill fromcnt5
. Else returnfalse
. - Else, if there are at least 1
$10
bill and 1$5
bill, delete 1 bill fromcnt10
andcnt5
. Else if there are more than 3$5
bills, delete 3 bills fromcnt5
. Else, returnfalse
.
- If the bill equals to 5, add it to
- If all the bills can be given correct change, return
true
.
Solution
class Solution {
public boolean lemonadeChange(int[] bills) {
int cnt5 = 0;
int cnt10 = 0;
for (int bill: bills) {
if (bill == 5)
cnt5++;
else if (bill == 10) {
if (cnt5 > 0)
cnt5--;
else
return false;
cnt10++;
}
else {
if (cnt10 > 0 && cnt5 > 0) {
cnt10--;
cnt5--;
}
else if (cnt5 >= 3) {
cnt5 -= 3;
}
else
return false;
}
}
return true;
}
}
406. Queue Reconstruction by Height
Link: https://leetcode.com/problems/queue-reconstruction-by-height/
Description
You are given an array of people, people
, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki]
represents the ith
person of height hi
with exactly ki
other people in front who have a height greater than or equal to hi
.
Reconstruct and return the queue that is represented by the input array people
. The returned queue should be formatted as an array queue
, where queue[j] = [hj, kj]
is the attributes of the jth
person in the queue (queue[0]
is the person at the front of the queue).
Approach
- Sort the array in descending order. First compare
hi
. If thehi
are the same, then compareki
. - Add the person into the list to the
kth
position.
Solution
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a, b) -> {
if (a[0] == b[0])
return a[1] - b[1];
else
return b[0] - a[0];
});
List<int[]> list = new ArrayList<>();
for (int[] p: people)
list.add(p[1], p);
return list.toArray(new int[people.length][]);
}
}
452. Minimum Number of Arrows to Burst Balloons
List: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
Description
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points
where points[i] = [xstart, xend]
denotes a balloon whose horizontal diameter stretches between xstart
and xend
. You do not know the exact y-coordinates of the balloons.
Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart
and xend
is burst by an arrow shot at x
if xstart <= x <= xend
. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
Given the array points
, return the minimum number of arrows that must be shot to burst all balloons.
Approach
- Sort
point
in ascending order according toxstart
. - Loop through the sorted
point
from the secoond element:- If the start of the element is larger than the end of the previous element, it means it needs one more shot.
- Else, update the end of the element to be the minimum of its current end point and the end point of the previous balloon.
Solution
class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
int cnt = 1;
for (int i = 1; i < points.length; i++) {
if (points[i - 1][1] < points[i][0])
cnt++;
else
points[i][1] = Math.min(points[i][1], points[i - 1][1]);
}
return cnt;
}
}