Skip to content

AswinBarath/Dynamic-Programming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Dynamic Programming

Problems based on the Dynamic Programming approach

SDE Sheet problems on Dynamic Programming

Sheet Link

Day 25

Completion Status Problems on Arrays Explanation Solution
πŸ”ƒ Max Product Subarray Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Longest Increasing Subsequence Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Longest Common Subsequence Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ 0-1 Knapsack Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Edit Distance Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Maximum sum increasing subsequence Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Matrix Chain Multiplication Brute, Better & Optimal Approaches Java Soultion

Day 26

Completion Status Problems on Arrays Explanation Solution
πŸ”ƒ Maximum sum path in the matrix Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Coin change Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Subset Sum Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Rod Cutting Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Egg Dropping Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Word Break Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Palindrome Partitioning Brute, Better & Optimal Approaches Java Soultion
πŸ”ƒ Maximum profit in Job scheduling Brute, Better & Optimal Approaches Java Soultion

Task list


What is Dynamic Programming?

  • General, powerful algorithm design technique

  • Dynamic Programming was invented by a guy named Richard Bellman

  • We can solve problems by polynomial time using DP

  • Intended for optimization problems

    • Like shortest paths, minimum-length path ( to find the best way to do something)
    • Minimize or maximaze something (optimization problem)
  • DP => recursion + memoization

  • DP => subproblems + "reuse"


Top-Down approach

	     Fib(4)
                 /    \
            Fib(3)    Fib(2)
           /     \     /   \
      Fib(2)  Fib(1) Fib(1)  Fib(0)
     /    \
Fib(1) Fib(0)

Bottom-up approach

Fib(0) Fib(1) Fib(2) Fib(3) Fib(4)

DP Patterns

Fibonacci Numbers Pattern

  • F(0) = 0, F(1) = 1
  • F(n) = F(n-1) + F(n-2) -> {for n >= 2}
  • Example: Fibonacci(6)
F(6) F(5) F(4) F(3) F(2) F(1) F(0)
  • We have 1 parameter which is the prevoius sum, hence it's 1-D memoization/cache

Problems:


Fibonacci Number

  • Approach:
    • Use two recursive calls for finding F(n-1) and F(n-2)
    • Add them to get F(n) and to store F(n) in an array (memoization)

Climbing Stairs Amazon

  • Use two recursive calls for taking 1 step and 2 steps
  • Add both of the count and to store the distinct no. of count in an array (memoization)

House Robber Google

Maximum Alternating Subsequence Sum Facebook, Amazon, Google


Zero / One Knapsack pattern

  • Example: coins = [1, 2, 3] target = 5
  • We can only use the coins 0 or 1 times (0/1 Knapsack)
  • The goal of the problem is to sum up for the target value
  • We have 2 parameters which are coins & target, hence it's 2-D memoization/cache
\ Target 5 4 3 2 1 0
Coins 1 T
2 T
3 T

Problems:

Partition Equal Subset Sum Facebook, Microsoft, Uber, Yahoo


Unbounded Knapsack pattern

  • Example: coins = [1, 2, 3] target = 5
  • Similar to 0/1 Knapsack problem but the only difference is we can choose coins for infinte amount of times (Unbounded knapsack)
  • The goal of the problem (coins example) is still the same where we need to sum up for the target value
  • We have 2 parameters which are coins & target, hence it's 2-D memoization/cache
\ Target 5 4 3 2 1 0
Coins 1 T
2 T
3 T

Problems:


Longest Common Subsequence (LCS) pattern

  • Example: "abc" , "ace"
  • Subsequence is choice of characters stricly following order and not consecutive
  • LCS have 2 parameters which are the given two strings, hence it's 2-D memoization/cache
  • Base case is the end of the strings
\ a b c
a x
c x
e x
x x x 0

Problems:


Palindromes (pattern)

  • Example: "racecar"
  • We can choose a substring like "e" and expand outwards to check other substrings
  • Along the way we can store the result in an array (1-D memoization/cache)
  • Similarly we can choose two characters "ce" for even length palindromes

Problems:


About

Problems based on Dynamic Programming and various patterns

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages