-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 17: Dynamic Programming
- Loading branch information
Showing
27 changed files
with
752 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Chapter 17: Dynamic Programming | ||
|
||
* 17.1 ComputeScoreCombinations | ||
* 17.2 ComputeLevenshtein | ||
* 17.3 CountPossibleTraversals | ||
* 17.4 ComputeBinomialCoefficients | ||
* 17.5 SearchSequence2D | ||
* 17.6 KnapsackProblem | ||
* 17.7 BedBathBeyondProblem | ||
* 17.8 MinimumWeightPathTriangle | ||
* 17.9 MaximumCoinsGain | ||
* 17.10 CountMovesToClimbStairs | ||
* 17.11 PrettyPrintingProblem | ||
* 17.12 LongestNondecreasingSubsequence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>elements-of-programming-interviews</artifactId> | ||
<groupId>gardncl</groupId> | ||
<version>1.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>dynamicprogramming</artifactId> | ||
<name>Chapter 17: Dynamic Programming</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>datastructures</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
20 changes: 20 additions & 0 deletions
20
dynamicprogramming/src/main/java/BedBathBeyondProblem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class BedBathBeyondProblem { | ||
|
||
/* | ||
17.7 | ||
Given a dictionary, i.e., a set of strings, and a name, | ||
design an efficient algorithm that checks whether the | ||
name is the concatenation of a sequence of dictionary | ||
words. If such a concatenation exists, return it. | ||
*/ | ||
|
||
public static List<String> decompose(String domain, Set<String> dictionary) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
dynamicprogramming/src/main/java/ComputeBinomialCoefficients.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class ComputeBinomialCoefficients { | ||
|
||
/* | ||
17.4 | ||
Design an efficient algorithm for computing (n choose k) | ||
which has the property that it never overflows if the | ||
final result fits in the integer work size. | ||
*/ | ||
|
||
public static int compute(int n, int k) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class ComputeLevenshtein { | ||
|
||
/* | ||
17.2 | ||
Write a program that takes two strings and | ||
computes the minimum number of edits needed | ||
to transform the first string into the | ||
second string. | ||
*/ | ||
|
||
public static int levenschteinDistance(String A, String B) { | ||
|
||
return 0; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
dynamicprogramming/src/main/java/ComputeScoreCombinations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class ComputeScoreCombinations { | ||
|
||
/* | ||
17.1 | ||
Write a program that takes a final score and scores for | ||
individual plays, and returns the number of combinations | ||
of plays that result in teh final score. | ||
*/ | ||
|
||
public static int compute(int finalScore, List<Integer> playScores) { | ||
|
||
return 0; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
dynamicprogramming/src/main/java/CountMovesToClimbStairs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class CountMovesToClimbStairs { | ||
|
||
/* | ||
17.10 | ||
Write a program which takes as inputs n and k and | ||
returns the number of ways in which you can get | ||
to your destination. | ||
*/ | ||
|
||
public static int numberOfWays(int top, int maximumStep) { | ||
|
||
return 0; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
dynamicprogramming/src/main/java/CountPossibleTraversals.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class CountPossibleTraversals { | ||
|
||
/* | ||
17.3 | ||
Write a program that counts how many ways you | ||
can go from the top-left to the bottom-right | ||
in a 2D array. | ||
*/ | ||
|
||
public static int numberOfWays(int n, int m) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import java.util.List; | ||
|
||
public class KnapsackProblem { | ||
|
||
/* | ||
17.6 | ||
Write a program for the knapsack problem that selects a | ||
subset of items that has maximum value and satisfies | ||
the weight constraint. | ||
Tuple -> (cost,weight) | ||
*/ | ||
|
||
public static int compute(List<Tuple> items, int capacity) { | ||
|
||
return 0; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
dynamicprogramming/src/main/java/LongestNondecreasingSubsequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class LongestNondecreasingSubsequence { | ||
|
||
/* | ||
17.12 | ||
Write a program that takes as input an array of numbers and | ||
returns the length of a longest nondecreasing subsequence | ||
in the array. | ||
*/ | ||
|
||
public static int compute(List<Integer> A) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.List; | ||
|
||
public class MaximumCoinsGain { | ||
|
||
/* | ||
17.9 | ||
Design an efficient algorithm for computing the maximum | ||
total value for the starting player in the pick-up-coins game. | ||
*/ | ||
|
||
public static int computeMaximum(List<Integer> coins) { | ||
|
||
return 0; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
dynamicprogramming/src/main/java/MinimumWeightPathTriangle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.List; | ||
|
||
public class MinimumWeightPathTriangle { | ||
|
||
/* | ||
17.8 | ||
Write a program that takes as input a triangle of | ||
numbers and returns the weight of a minimum weight path. | ||
*/ | ||
|
||
public static int minimum(List<List<Integer>> triangle) { | ||
|
||
return 0; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
dynamicprogramming/src/main/java/PrettyPrintingProblem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.List; | ||
|
||
public class PrettyPrintingProblem { | ||
|
||
/* | ||
17.11 | ||
Given text, i.e., string of words separated by single blanks, | ||
decompose the text into lines such that no word is split | ||
across lines and the messiness of the decomposition is minimized. | ||
Each line can hold no more than a specified number of characters. | ||
*/ | ||
|
||
public static int minimumMessiness(List<String> words, int lineLength) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class SearchSequence2D { | ||
|
||
/* | ||
17.5 | ||
Write a program that takes as arguments a 2D array | ||
and a 1D array, and checks whether the 1D array | ||
occurs in the 2D array. | ||
*/ | ||
|
||
public static boolean isContained(List<List<Integer>> grid, List<Integer> pattern) { | ||
|
||
return false; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
dynamicprogramming/src/test/java/BedBathBeyondProblemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class BedBathBeyondProblemTest { | ||
|
||
private List<String> expected; | ||
private String domain; | ||
private Set<String> dictionary; | ||
|
||
@Test | ||
public void decompose1() throws Exception { | ||
expected = Arrays.asList( | ||
"a", | ||
"man", | ||
"a", | ||
"plan", | ||
"a", | ||
"canal" | ||
); | ||
domain = "amanaplanacanal"; | ||
dictionary = new HashSet<>(Arrays.asList( | ||
"a", | ||
"man", | ||
"plan", | ||
"canal" | ||
)); | ||
|
||
test(expected, domain, dictionary); | ||
} | ||
|
||
private static void test(List<String> expected, String domain, Set<String> dictionary) { | ||
assertEquals(expected, BedBathBeyondProblem.decompose(domain, dictionary)); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
dynamicprogramming/src/test/java/ComputeBinomialCoefficientsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeBinomialCoefficientsTest { | ||
|
||
private int expected; | ||
private int n; | ||
private int m; | ||
|
||
@Test | ||
public void compute1() throws Exception { | ||
expected = 10; | ||
n = 5; | ||
m = 2; | ||
|
||
test(expected, n, m); | ||
} | ||
|
||
@Test | ||
public void compute2() throws Exception { | ||
expected = 850668; | ||
n = 42; | ||
m = 37; | ||
|
||
test(expected, n, m); | ||
} | ||
|
||
@Test | ||
public void compute3() throws Exception { | ||
expected = 245157; | ||
n = 23; | ||
m = 7; | ||
|
||
test(expected, n, m); | ||
} | ||
|
||
private void test(int expected, int n, int m) { | ||
assertEquals(expected, ComputeBinomialCoefficients.compute(n, m)); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
dynamicprogramming/src/test/java/ComputeLevenshteinTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeLevenshteinTest { | ||
|
||
private int expected; | ||
private String A; | ||
private String B; | ||
|
||
@Test | ||
public void levenschteinDistance1() throws Exception { | ||
expected = 4; | ||
A = "Saturday"; | ||
B = "Sunday"; | ||
|
||
test(expected, A, B); | ||
} | ||
|
||
@Test | ||
public void levenschteinDistance2() throws Exception { | ||
expected = 2; | ||
A = "book"; | ||
B = "back"; | ||
|
||
test(expected, A, B); | ||
} | ||
|
||
@Test | ||
public void levenschteinDistance3() throws Exception { | ||
expected = 9; | ||
A = "fantastic"; | ||
B = "excellent"; | ||
|
||
test(expected, A, B); | ||
} | ||
|
||
private void test(int expected, String A, String B) { | ||
assertEquals(expected, ComputeLevenshtein.levenschteinDistance(A,B)); | ||
} | ||
|
||
} |
Oops, something went wrong.