notes
  • computer-networking
    • extend-wifi-with-router
    • how-the-internet-works
    • idk
    • networking-devices
    • osi-model
    • tcp-ip
    • Types of VPN
  • databases
    • Foreign Keys
    • Redis
    • simple-queries
  • devops
    • ansible
    • Manual deployment
    • docker
    • Workflow file
    • nginx
    • promethues-grafana
    • terraform
  • hardware
    • Power
  • home-server
    • proxmox-basics
    • proxmox-setup
    • storage
  • languages-frameworks
    • programming-paradigms
    • programming-languages
      • Regex Notes
      • c
        • basics
        • pointers-memory
      • cpp
        • basics
        • running-cpp
      • php
        • basics
        • choizez
        • frameworks
          • laravel
      • python
        • venv
        • concepts
          • Using lambda
        • frameworks
          • django
            • django
            • start
      • java
        • advanced
          • functional-programming
          • reactive-programming
        • concepts
          • how-java-works
          • serialization
          • sockets
          • threads
        • extra
          • collection-framework
          • generics-and-wildcards
          • Regular Expressions (Regex)
          • streams
        • frameworks
          • orm
        • fundamentals
          • OOP
          • conditionals
          • data-structures
          • data-types
          • exceptions
          • files
          • Functions (aka method)
          • Loops
          • packages
          • type-casting
      • javascript
        • frameworks
          • morgan
          • Using Sequelize with PostgreSQL in JavaScript
  • operating-system
    • basics
    • linux-directories
    • Basic Unix Terminal Commands
  • others
    • dark-web
    • piracy
  • system-design
    • system-design
  • web-dev
    • full-stack
  • books
    • sicp
      • Recursion thought process
      • 1
        • 1.1
        • 1.2
        • 1.3
      • 2
        • 2.1
  • certifications
    • aws-certified-cloud-practitioner
      • core-services
      • other-services
    • comptia-a+
      • Cloud
      • hardware
      • Important terms
      • Important terms
      • Troubleshooting
  • cloud
    • aws
      • aws-cli
      • aws-ec2-deployment
  • dsa
    • algorithms
      • back-tracking
      • bfs
      • Binary Search
      • bit-manipulation
      • Bubble sort
      • bucket-sort
      • counting-sort
      • dfs
      • Divide & Conquer
      • djikstras-algorithm
      • dynamic-programming
      • external-sorting
      • greedy-algorithm
      • Heap sort
      • Insertion sort
      • kadanes-algorithm
      • Merge sort
      • Permutation
      • quick-sort
      • Radix Sort
      • recurrence-relation
      • recursion
      • Selection sort
      • sliding-window
      • subset
      • time-space-complexity
      • topological-sort
      • tree-traversals
      • Two Pointers Technique
    • data-structures
      • data-structures
  • security
    • authentication
      • What is JWT (JSON Web Token)?
    • software-architecture-design
      • design-patterns
Powered by GitBook
On this page
  • Dynamic Programming
  • When to use?
  • Optimal substructure
  • Overlapping sub-problems
  • Dynamic Programming Approaches
  • Top-down ( Memoization )
  • Bottom-up ( Tabulation)
  • Multidimensional DP
  1. dsa
  2. algorithms

dynamic-programming

Dynamic Programming

Solves a problem by dividing into smaller sub-problems

Stores solution of sub-problems to avoid solving more than one once

Dynamic programming is a technique used to optimise a recursive solution that does repeated work by storing solution of sub-problems , avoiding to solve them more than once

Keep thinking of not doing repeated work

When to use?

Optimal substructure

  • What is it?

    A problem has a optimal substructure if :

    The optimal solution of problem can be calculated from optimal sub-problems

Overlapping sub-problems

  • What is it?

    A problem has a overlapping sub-problems if :

    During the recursive process of dividing into subproblems , solve the same sub-problems more than once

# Brute Force Recursion
def bruteForce(n):
    if n <= 1:
        return n
    return bruteForce(n - 1) + bruteForce(n - 2)

Dynamic Programming Approaches

Top-down ( Memoization )

  • What is it ?

    Solve problems using by storing the results of recursively called functions

    • Why is it called the top down approach?

      Starts by trying to solve the initial problem if not solve sub-problems

# Memoization by using hashmap as cache to solve fibonnacci
def memoization(n, cache):
    if n <= 1:
        return n
    if n in cache:
        return cache[n]

    cache[n] = memoization(n - 1) + memoization(n - 2)
    return cache[n]

Bottom-up ( Tabulation)

  • What is it ?

    Solve problems using by storing solutions of smaller subproblems to use to solve the main problem iteratively.

# Tabulation
def dp(n):
    if n < 2:
        return n

    dp = [0, 1]
    i = 2
    while i <= n:
        tmp = dp[1]
        dp[1] = dp[0] + dp[1]
        dp[0] = tmp
        i += 1
    return dp[1]

Multidimensional DP

1D DP - 1 variable

2D DP - 2 variable

Previousdjikstras-algorithmNextexternal-sorting

Last updated 1 month ago

What is dynamic programming? - Inside code
Introduction to Multi-dimensional Dynamic Programming
Untitled
Untitled