A demonstration of dynamic programming through memoization in Java.
- RecursiveScore.java: Simple recursive implementation with performance measurement
- README.md: Repository instructions and explanations
-
Clone this repository:
git clone https://github.com/emrgem/dp-demo.git -
Navigate to the project directory:
cd dp-demo -
Compile the code:
javac RecursiveScore.java -
Run the program:
java RecursiveScore
This repository demonstrates optimization through dynamic programming.
Our recursive function gameScore(int level) calculates a player's score based on their level:
public static int gameScore(int level) {
// Base cases
if (level <= 0) return 0;
if (level == 1) return 5;
// Recursive calls
return 2 * gameScore(level - 1) + gameScore(level - 2);
}We'll implement memoization to avoid redundant calculations by:
- Adding a cache to store calculated results
- Checking the cache before performing calculations
- Storing results in the cache after calculation
After implementing memoization:
- Function calls will be reduced dramatically
- Time complexity will improve
- We'll be able to calculate values that were previously impractical
- Analyze the original function
- Add a counter to measure function calls
- Create a memoized version of the function
- Compare performance between original and memoized implementations
- Discuss real-world applications of dynamic programming