Skip to content

Commit b683923

Browse files
authored
Merge pull request #72 from Jer3myYu/java-solutions-greedy
Java Chapter 16: Greedy
2 parents 7e601cc + 3769853 commit b683923

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

java/Greedy/Candies.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Arrays;
2+
3+
public class Candies {
4+
public int candies(int[] ratings) {
5+
int n = ratings.length;
6+
// Ensure each child starts with 1 candy.
7+
int[] candies = new int[n];
8+
Arrays.fill(candies, 1);
9+
// First pass: for each child, ensure the child has more candies
10+
// than their left-side neighbor if the current child's rating is
11+
// higher.
12+
for (int i = 1; i < n; i++) {
13+
if (ratings[i] > ratings[i - 1]) {
14+
candies[i] = candies[i - 1] + 1;
15+
}
16+
}
17+
// Second pass: for each child, ensure the child has more candies
18+
// than their right-side neighbor if the current child's rating is
19+
// higher.
20+
for (int i = n - 2; i >= 0; i--) {
21+
if (ratings[i] > ratings[i + 1]) {
22+
// If the current child already has more candies than their
23+
// right-side neighbor, keep the higher amount.
24+
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
25+
}
26+
}
27+
return Arrays.stream(candies).sum();
28+
}
29+
}

java/Greedy/GasStations.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Arrays;
2+
3+
public class GasStations {
4+
public int gasStations(int[] gas, int[] cost) {
5+
// If the total gas is less than the total cost, completing the
6+
// circuit is impossible.
7+
if (Arrays.stream(gas).sum() < Arrays.stream(cost).sum()) {
8+
return -1;
9+
}
10+
int start, tank;
11+
start = tank = 0;
12+
for (int i = 0; i < gas.length; i++) {
13+
tank += gas[i] - cost[i];
14+
// If our tank has negative gas, we cannot continue through the
15+
// circuit from the current start point, nor from any station
16+
// before or including the current station 'i'.
17+
if (tank < 0) {
18+
// Set the next station as the new start point and reset the
19+
// tank.
20+
start = i + 1;
21+
tank = 0;
22+
}
23+
}
24+
return start;
25+
}
26+
}

java/Greedy/JumpToTheEnd.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class JumpToTheEnd {
2+
public boolean jumpToTheEnd(int[] nums) {
3+
// Set the initial destination to the last index in the array.
4+
int destination = nums.length - 1;
5+
// Traverse the array in reverse to see if the destination can be
6+
// reached by earlier indexes.
7+
for (int i = nums.length - 1; i >= 0; i--) {
8+
// If we can reach the destination from the current index,
9+
// set this index as the new destination.
10+
if (i + nums[i] >= destination) {
11+
destination = i;
12+
}
13+
}
14+
// If the destination is index 0, we can jump to the end from index
15+
// 0.
16+
return destination == 0;
17+
}
18+
}

0 commit comments

Comments
 (0)