My LeetCode Diary - Day35 Greedy Algo
860. Lemonade Change
class Solution {
public boolean lemonadeChange(int[] bills) {
int five = 0;
int ten = 0;
for (int i = 0; i < bills.length ; i++) {
if (bills[i] == 5) {
five++;
} else if (bills[i] == 10) {
five--;
ten++;
} else if (bills[i] == 20) {
if (ten>0) {
ten--;
five--;
} else{
five -=3;
}
}
if (five < 0 || ten < 0) {
return false;
}
}
return true;
}
}
406. Queue Reconstruction by Height
Clearly visualized explanation
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a,b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
List<int[]> ordered = new LinkedList<>();
for (int[] p: people) ordered.add(p[1], p);
return ordered.toArray(new int[people.length][2]);
}
452. Minimum Number of Arrows to Burst Balloons
class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
int count = 1;
for (int i = 1; i < points.length; i++) {
if (points[i][0] > points[i - 1][1]) {
count++;
} else {
points[i][1] = Math.min(points[i][1], points[i - 1][1]);
}
}
return count;
}
}