Skip to content

Java Chapter 16: Greedy #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions java/Greedy/Candies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Arrays;

public class Candies {
public int candies(int[] ratings) {
int n = ratings.length;
// Ensure each child starts with 1 candy.
int[] candies = new int[n];
Arrays.fill(candies, 1);
// First pass: for each child, ensure the child has more candies
// than their left-side neighbor if the current child's rating is
// higher.
for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
// Second pass: for each child, ensure the child has more candies
// than their right-side neighbor if the current child's rating is
// higher.
for (int i = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
// If the current child already has more candies than their
// right-side neighbor, keep the higher amount.
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
}
}
return Arrays.stream(candies).sum();
}
}
26 changes: 26 additions & 0 deletions java/Greedy/GasStations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Arrays;

public class GasStations {
public int gasStations(int[] gas, int[] cost) {
// If the total gas is less than the total cost, completing the
// circuit is impossible.
if (Arrays.stream(gas).sum() < Arrays.stream(cost).sum()) {
return -1;
}
int start, tank;
start = tank = 0;
for (int i = 0; i < gas.length; i++) {
tank += gas[i] - cost[i];
// If our tank has negative gas, we cannot continue through the
// circuit from the current start point, nor from any station
// before or including the current station 'i'.
if (tank < 0) {
// Set the next station as the new start point and reset the
// tank.
start = i + 1;
tank = 0;
}
}
return start;
}
}
18 changes: 18 additions & 0 deletions java/Greedy/JumpToTheEnd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class JumpToTheEnd {
public boolean jumpToTheEnd(int[] nums) {
// Set the initial destination to the last index in the array.
int destination = nums.length - 1;
// Traverse the array in reverse to see if the destination can be
// reached by earlier indexes.
for (int i = nums.length - 1; i >= 0; i--) {
// If we can reach the destination from the current index,
// set this index as the new destination.
if (i + nums[i] >= destination) {
destination = i;
}
}
// If the destination is index 0, we can jump to the end from index
// 0.
return destination == 0;
}
}