Skip to content

Instantly share code, notes, and snippets.

@phyous
Last active October 27, 2016 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phyous/b5b3031cd78040a1aed12975b1dfca35 to your computer and use it in GitHub Desktop.
Save phyous/b5b3031cd78040a1aed12975b1dfca35 to your computer and use it in GitHub Desktop.
Calculating Minimum number of hops through an array
import java.util.ArrayList;
import java.util.List;
public class MinHopsNovel {
public static List<Integer> minHops(List<Integer> input) {
List<Integer> ret = new ArrayList<>();
if (input.size() == 0) return ret;
int currentPosition = 0;
int endPosition = input.size() - 1;
while (true) {
if (currentPosition == endPosition) break;
currentPosition = getBestNextStep(input, currentPosition);
ret.add(input.get(currentPosition));
}
return ret;
}
/**
* Given an array (input) and a position in the list (start),
* compute the farthest jumpable position (farthestPos) = start+input[start]
* The best next step is the one that can get us the farthest from farthestPos.
* e.g: input = [3, 3, 5, 3, 0]
* start = pos 0
* farthestPos = 0 + 3 = 3
* pos 3 gets us up to pos 3+3=6 if we select it
* pos 2 gets us up to pos 2+5=7 if we select it
* pos 1 gets us up to pos 1+3=4 if we select it
*
* Therefore, pos 2 is the best choice for this iteration since
* it opens up the most future possibilities
*/
public static Integer getBestNextStep(List<Integer> input, Integer start) {
int allowedHops = input.get(start);
int farthestPos = start + allowedHops;
int endPos = input.size() - 1;
// If we can hop straight to the end, do it.
if (farthestPos >= endPos) return endPos;
int bestPos = farthestPos;
int bestPosVal = Integer.MIN_VALUE;
for (int i = farthestPos; i > start; i--) {
int newBestPosVal = i + input.get(i);
if (newBestPosVal > bestPosVal) {
bestPosVal = newBestPosVal;
bestPos = i;
}
}
return bestPos;
}
}
import com.sun.tools.javac.util.List;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MinHopsTest {
@Test
public void testNovel() {
// Basic input test case
List<Integer> test = List.of(1, 2, 5, 1, 1, 0);
List<Integer> expected = List.of(2, 5, 0);
assertEquals(expected, MinHopsNovel.minHops(test));
// Test that an input that has a single hop to the end works
test = List.of(10, 5, 5, 5, 5, 0);
expected = List.of(0);
assertEquals(expected, MinHopsNovel.minHops(test));
// Test all 1's until the end
test = List.of(1, 1, 1, 0);
expected = List.of(1, 1, 0);
assertEquals(expected, MinHopsNovel.minHops(test));
// Single hop list
test = List.of(1, 0);
expected = List.of(0);
assertEquals(expected, MinHopsNovel.minHops(test));
// Test biggest first hop is to a smaller number
test = List.of(3, 2, 1, 1, 0);
expected = List.of(1, 0);
assertEquals(expected, MinHopsNovel.minHops(test));
// Test best hop is to a smaller number
test = List.of(3, 3, 5, 3, 1, 1, 1, 1, 1, 0);
expected = List.of(5, 1, 1, 0);
assertEquals(expected, MinHopsNovel.minHops(test));
// Test best hop is to a smaller number
test = List.of(6, 3, 1, 1, 1, 2, 1, 0);
expected = List.of(1, 0);
assertEquals(expected, MinHopsNovel.minHops(test));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment