Day 26

Lemonade Change

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public boolean lemonadeChange(int[] bills) {
int five = 0;
int ten = 0;
for (int bill : bills) {
if (bill == 5) {
five++;
}

if (bill == 10) {
if (five == 0) {
return false;
}
ten++;
five--;
}

if (bill == 20) {
if (five >= 1 && ten >= 1) {
five--;
ten--;
} else if (five >= 3) {
five -= 3;
} else {
return false;
}

}
}
return true;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func lemonadeChange(bills []int) bool {
five, ten := 0, 0
for i := 0; i < len(bills); i++ {
if bills[i] == 5 {
five++
}

if bills[i] == 10 {
if five == 0 {
return false
}
ten++
five--
}

if bills[i] == 20 {
if five >= 1 && ten >= 1 {
ten--
five--
} else if five >= 3 {
five -= 3
} else {
return false
}
}
}
return true
}

Queue Reconstruction by Height

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a, b) -> {
if (a[0] == b[0]) {
return a[1] - b[1];
}
return b[0] - a[0];
});

LinkedList<int[]> queue = new LinkedList<>();

for (int[] p : people) {
queue.add(p[1], p);
}
return queue.toArray(new int[people.length][]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
func reconstructQueue(people [][]int) [][]int {
sort.Slice(people, func(i, j int) bool {
if people[i][0] == people[j][0] {
return people[i][1] < people[j][1]
}
return people[i][0] > people[j][0]
})

for i, p := range people {
copy(people[p[1]+1:i+1], people[p[1]:i+1])
people[p[1]] = p
}
return people
}

Minimum Number of Arrows to Burst Balloons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points, (a, b) -> {
return Integer.compare(a[0], b[0]);
});

int res = 1;
for (int i = 1; i < points.length; i++) {
if (points[i][0] > points[i - 1][1]) {
res++;
} else {
points[i][1] = Math.min(points[i][1], points[i - 1][1]);
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func findMinArrowShots(points [][]int) int {
sort.Slice(points, func(i, j int) bool {
return points[i][0] < points[j][0]
})

res := 1
for i := 1; i < len(points); i++ {
if points[i][0] > points[i-1][1] {
res++
} else {
if points[i-1][1] > points[i][1] {
points[i][1] = points[i][1]
} else {
points[i][1] = points[i-1][1]
}

}
}
return res
}