From fc1205415d910c5580ac8265ec57c7086c4e42bf Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Thu, 6 Feb 2025 10:45:28 -0500 Subject: [PATCH 1/3] add: JumpToTheEnd --- java/Greedy/JumpToTheEnd.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 java/Greedy/JumpToTheEnd.java diff --git a/java/Greedy/JumpToTheEnd.java b/java/Greedy/JumpToTheEnd.java new file mode 100644 index 0000000..0ebafa7 --- /dev/null +++ b/java/Greedy/JumpToTheEnd.java @@ -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; + } +} From 0c657846f9b196c2676fa862d415dc7531fd262b Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Thu, 6 Feb 2025 10:50:31 -0500 Subject: [PATCH 2/3] add: GasStations --- java/Greedy/GasStations.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 java/Greedy/GasStations.java diff --git a/java/Greedy/GasStations.java b/java/Greedy/GasStations.java new file mode 100644 index 0000000..15aa7d4 --- /dev/null +++ b/java/Greedy/GasStations.java @@ -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; + } +} From 3769853364d1c05d1d2e040c677a279b9e223e53 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Thu, 6 Feb 2025 10:58:31 -0500 Subject: [PATCH 3/3] add: Candies --- java/Greedy/Candies.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 java/Greedy/Candies.java diff --git a/java/Greedy/Candies.java b/java/Greedy/Candies.java new file mode 100644 index 0000000..2b02f59 --- /dev/null +++ b/java/Greedy/Candies.java @@ -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(); + } +}