avatarNaina Chaturvedi

Summary

The provided content outlines an in-depth exploration of dynamic programming (DP) techniques, specifically focusing on one-dimensional (1D) DP problems, and offers insights into system design case studies.

Abstract

The web content delves into the concept of dynamic programming, emphasizing 1D dynamic programming problems, which are characterized by the change of a single parameter during recursion. It explains the importance of DP in solving problems with overlapping subproblems and optimal substructure. The article covers key DP patterns and techniques, provides examples of common DP problems such as the House Robber II, Coin Change, and Binary Tree Maximum Path Sum, and offers tips for solving such problems efficiently. Additionally, it introduces a series of system design case studies and resources for further learning in data science, machine learning, and software development. The content also promotes a YouTube channel, Ignito, which provides video explanations and project tutorials related to the topics discussed.

Opinions

  • The author believes that dynamic programming is a highly important technique for problem-solving in computer science, particularly for optimization and combinatorial problems.
  • There is an emphasis on learning by doing, as evidenced by the inclusion of daily updated DP questions with solutions and the encouragement to subscribe for video explanations.
  • The content suggests that understanding the basics of system design is crucial, offering a comprehensive series on the subject.
  • The author values the sharing of knowledge and resources, providing links to a wide range of projects, series, and best practices in data science, machine learning, and system design.
  • The author is enthusiastic about the launch of the Ignito YouTube channel and its potential to enhance learning through video content and project implementations.
  • The content conveys a commitment to continuous learning and improvement, with daily updates and encouragement for reader engagement and feedback.

Day 7 of 30 days of Data Structures and Algorithms and System Design Simplified — 1 D Dynamic Programming

Pic credits : Nimbus

Welcome back peeps. Hope all’s well. In this post we will cover Dynamic Programming technique ( 1D) as follows —

What and Why Dynamic Programming technique(in 2–3 sentences)?

How does Dynamic Programming technique work?

Important Patterns and Techniques in Dynamic Programming technique Questions

Most Important Questions with Solutions

Complexity Analysis

Tips and Techniques to solve Dynamic Programming technique Questions Fast.

Projects Videos —

All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).

Subscribe today!

System Design Case Studies — In Depth

Design Instagram

Design Messenger App

Design Twitter

Design URL Shortener

Design Dropbox

Design Youtube

Design API Rate Limiter

Design Web Crawler

Design Facebook’s Newsfeed

Design Yelp

Design Uber

Design Tinder

Design Tiktok

Design Whatsapp

Most Popular System Design Questions

Mega Compilation : Solved System Design Case studies

Dynamic Programming

Importance : Very High

Day 2 of data structures and algorithms covers the topics that are most important and with highest ROI.

Note : New Dynamic Programming questions with solutions will be added everyday. So keep checking this post everyday.

Let’s dive in!

What is Dynamic Programming?

Dynamic programming is a technique for solving problems which are defined by recurrences with overlapping sub problems.

This technique solves the class of problems that have overlapping subproblems and optimal substructure property.

The problems are solved in bottom up manner and the optimal solution is constructed using the computed values/solution of the sub problems.

Examples of dynamic programming problems —

Longest Common Subsequence Problem.

Shortest Common Super sequence Problem.

Longest Increasing Subsequence Problem.

Matrix Chain Multiplication Problem.

0–1 Knapsack Problem.

Partition Problem etc

There are two types of DP problems —

  • 1D DP problems

The types of problems in which during recursion only one parameter changes its values.

Pic credits : gohired
  • 2D DP problems

The types of problems in which during recursion two parameter change their values.

Pic credits : ResearchGate

In this post we will see 1D Dynamic programming problems in detail.

How does Dynamic Programming work?

Dynamic Programming is a technique for solving problems by breaking them down into smaller subproblems and storing the solutions to those subproblems to avoid redundant work. This technique is often used to solve problems that have overlapping subproblems, which can be identified by a recursive structure.

The dynamic programming technique typically involves breaking the problem down into smaller subproblems, solving each subproblem and storing the result in a table or an array. The solutions to the subproblems are then used to solve the larger problem, by combining or modifying the stored solutions in some way.

The main gist is — solve all the sub problems, make decisions at every stage and then select one that helps to find the most optimal solution.

In this sub problems are dependent which means the sub problems share sub-sub problems and every sub-sub problem is solved just once and the solutions to sub-sub problems are stored in a table and later used to solve the higher level sub problems.

Pic credits : Devcommunity

Important Patterns and Techniques in Dynamic Programming Questions

Important patterns and techniques in dynamic programming questions include identifying the overlapping subproblems, understanding how the subproblems relate to the original problem, and recognizing the appropriate time to use dynamic programming.

Dynamic Programming problems can be divided into two types: optimization problems and combinatoric problems.

To solve Dynamic programming problem —

1.Divide the problem into subproblem and find a recursive ( best is recursive DFS) relation.

2. Find the optimal solutions of the sub‐ problems and approach bottom up.

3. Use memoization to eliminate redundancy i.e save the intermediate results and cache them.

Patterns → Questions like below belong to Dynamic Programming technique( not limited to):

Min/Max Cost

Path Sum

Coin Change

1 D Path/houses to rob

Paint houses/fences

Flower the garden ( max flowers that you can plant)

Longest Palindromic substring etc

Most Important Questions with Solutions

Note : New Dynamic Programming questions with solutions are added every day. So keep checking this post day.

Golden rule is — Learn by doing/implementing

In this we will see most important Dynamic Programming questions.

House Robber II

Question —

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Example :

Input: nums = [2,3,2]
Output: 3

Solution :

Main Logic/Idea —

The main logic is to use array slicing to make sure robing first house doesn’t overlap with the last house. You can either rob the first house + n houses or start robbing from the second house and keep updating the house rob variable.

Main Logic

Implementation —

def rob(self, nums: List[int]) -> int:
        return max(nums[0],self.robhelper(nums[1:]),self.robhelper(nums[:-1]))
    
    
    def robhelper(self,nums):
        rob1,rob2 = 0,0
        
        for n in nums:
            ans = max( rob1+n, rob2)
            rob1 = rob2
            rob2 = ans
        return rob2

Question Link

Similar Pattern —

Paint Fence

Non-negative Integers without Consecutive Ones

Coin Path

Paint House

Full Code Video Explanation ( In progress. Subscribe today for updates) :

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Coin Change

Question —

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

You may assume that you have an infinite number of each kind of coin.

Example :

Input: coins = [1,2,5], amount = 11
Output: 3

Solution :

Main Logic/Idea —

The main logic is to see how to make the total amount with one coin and then minus that coin from the total amount to make that amount with other coins. For example dp[7] = 1+dp[7–3] assuming the coin denomination is 3.

Main Logic

Implementation —

def coinChange(self, coins: List[int], amount: int) -> int:
        ans = [amount +1 ] * (amount +1)
        ans[0] = 0
        
        for amt in range( 1, amount+1):
            for cn in coins :
                if amt - cn >= 0:
                    ans[amt] = min(ans[amt], 1+ans[amt-cn])
        return ans[amount] if ans[amount] != amount +1 else -1

Question Link

Similar Pattern —

Maximum Value of K Coins From Piles

Minimum Number of Operations to Convert Time

Minimum Cost For Tickets

Full Code Video Explanation ( In progress. Subscribe today for updates) :

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Binary Tree Maximum Path Sum

Question —

A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.

The path sum of a path is the sum of the node’s values in the path.

Given the root of a binary tree, return the maximum path sum of any non-empty path.

Example :

Input: root = [1,2,3]
Output: 6

Solution :

Main Logic/Idea —

The main logic is to calculate the path with or without splitting. Calculate the left path and the right path and add it to the node value. Then find the max of path obtained with or without splitting.

Main Logic

Implementation —

def maxPathSum(self, root: Optional[TreeNode]) -> int:
        ans = [root.val]
        
        def calpath(node):
            if not node: return 0
            
            lMax = calpath(node.left)
            rMax = calpath(node.right)
            lMax = max(lMax,0)
            rMax = max(rMax,0)
            
            ans[0] = max(ans[0], node.val + lMax + rMax)
            
            return node.val + max(lMax,rMax)
        calpath(root)
        return ans[0]

Question Link

Similar Pattern —

Longest Univalue Path

Time Needed to Inform All Employees

Path Sum IV

Full Code Video Explanation ( In progress. Subscribe today for updates) :

Note : New Dynamic Programming questions with solutions are added every day. So keep checking this post day.

Tips and Techniques to solve 1-D Dynamic Programming Questions Fast —

Remember the gist of dynamic problem is to find the solution to the sub sequence/sub problems.

Most of the Dynamic Programming problems appear with optimal substructures where by optimally solving a sequence of local problems, one can reach to a globally optimal solution.

To solve the dynamic programming questions fast —

First create a solution with decision tree.

See which part of the decision tree in repeating for the sub problems.

Cache/Memoize the part that’s repeating and then find the optimal solution to the subproblem

Once optimal solutions to all the subproblems are calculated then combine them to develop the globally optimal solution.

That’s it for now. Day 8 : Two pointer Technique coming soon !

Let me know if you have questions in the comment section below. Subscribe/ Follow, Like/Clap as it will encourage me to write more in my free time and Stay Tuned!!

Read More —

11 most important System Design Base Concepts

1. System design basics

2. Horizontal and vertical scaling

3. Load balancing and Message queues

4. High level design and low level design, Consistent Hashing, Monolithic and Microservices architecture

5. Caching, Indexing, Proxies

6. Networking, How Browsers work, Content Network Delivery ( CDN)

7. Database Sharding, CAP Theorem, Database schema Design

8. Concurrency, API, Components + OOP + Abstraction

9. Estimation and Planning, Performance

10. Map Reduce, Patterns and Microservices

11. SQL vs NoSQL and Cloud

12. Most Popular System Design Questions

13. System Design Template — How to solve any System Design Question

14. Quick RoundUp : Solved System Design Case Studies

Some of the other best Series —

60 days of Data Science and ML Series with projects

30 Days of Natural Language Processing ( NLP) Series

30 days of Machine Learning Ops

30 days of Data Structures and Algorithms and System Design Simplified

60 Days of Deep Learning with Projects Series

30 days of Data Engineering with projects Series

Data Science and Machine Learning Research ( papers) Simplified **

100 days : Your Data Science and Machine Learning Degree Series with projects

23 Data Science Techniques You Should Know

Tech Interview Series — Curated List of coding questions

Complete System Design with most popular Questions Series

Complete Data Visualization and Pre-processing Series with projects

Complete Python Series with Projects

Complete Advanced Python Series with Projects

Kaggle Best Notebooks that will teach you the most

Complete Developers Guide to Git

Exceptional Github Repos — Part 1

Exceptional Github Repos — Part 2

All the Data Science and Machine Learning Resources

210 Machine Learning Projects

Tech Newsletter —

If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :

For Python Projects —

For complete 60 days of Data Science and ML : Day 1 — Day 60 : Quick Recap of 60 days of Data Science and ML

Follow for more updates. Stay tuned and keep coding!

For other projects, tune to —

Build Machine Learning Pipelines( With Code)

Recurrent Neural Network with Keras

Clustering Geolocation Data in Python using DBSCAN and K-Means

Facial Expression Recognition using Keras

Hyperparameter Tuning with Keras Tuner

Custom Layers in Keras

Software Development
Tech
Programming
Data Science
Machine Learning
Recommended from ReadMedium