Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
dTub
Скачать

The 0/1 Knapsack Problem (Demystifying Dynamic Programming)

Back To Back SWE

Software Engineering

programming

programming interviews

coding interviews

coding questions

algorithms

algorithmic thinking

Google internship

Facebook internship

software engineering internship

swe internship

big n companies

dynamic programming

0/1 knapsack problem

knapsack problem

recursion

greedy algorithms

software engineering

Автор: Back To Back SWE

Загружено: 22 дек. 2018 г.

Просмотров: 218 504 просмотра

Описание:

👉 NEW VIDEO & CODE: https://backtobackswe.com/platform/co... (free)


Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions

I was inspired to do this video after seeing that Tuschar Roy had covered this problem. He did a good job, but I feel it very necessary to stress what is really happening and what each cell REALLY means.

Dynamic programming is about subproblems, not remembering patterns to fill cells in with. I watched EVERY ONE of Tuschar Roy's videos and found myself MEMORIZING how to fill out the cells INSTEAD of really knowing what was going on.

I hope this video sheds light on what this problem is really trying to express.

I talked about the bottom up way to do things. Here is the code for that way of doing it: https://www.sanfoundry.com/java-progr...

You can also do it TOP DOWN with recursion where we investigate all expressions of the subproblems to find the optimal solution. The book Elements of Programming Interviews by Aziz Adnan has a very good version of this. The problem is 17.6 in that book.

++++++++++++++++++++++++++++++++++++++++++++++++++


Question: Write a program for the knapsack problem that selects a subset of items that has maximum value and satisfies the weight constraint. All items have integer weights and values. Return the value of the subset.


Can we do it greedily?

0/1 means you cannot split an item. If you could split an item, you could solve this greedily by sorting the item entries by value and then picking from greatest value to least. When you run out of space in your "sack", you'd split the last item and then you would have maximized weight vs value.

Brute Force: We could consider all subsets of items in a complete search and take on the cost of exponential time of 2^n (we will explain this in another video).

Greedy doesn't work, brute forcing won't make the cut, now what? What can we do now?


Dynamic Programming.

Notice that we can subproblem this.

Dynamic programming is not about stupid magic tables that you see people fill out, it is not about guessing. DP is about remembering the solutions to subproblems so that we can find the globally optimal solution. We just subproblemed this recursively.

This is where the table comes from. Each cell MEANS SOMETHING.

IT IS THE ANSWER TO THE QUESTION.

If we solve all the subproblems and remember all answers then we will find the globally optimal answer.

The subproblems are represented by what is called a recurrence equation.


Complexities

n = total items
m = max weight (max weight constraint)

Time: O(nm) (we will be solving this many subproblems)
Space: O(nm) (we will store the results of n*m subproblems)


++++++++++++++++++++++++++++++++++++++++++++++++++

HackerRank:    / @hackerrankofficial  

Tuschar Roy:    / tusharroy2525  

GeeksForGeeks:    / @geeksforgeeksvideos  

Jarvis Johnson:    / vsympathyv  

Success In Tech:    / @successintech  

++++++++++++++++++++++++++++++++++++++++++++

The 0/1 Knapsack problem is question 17.6 in the fantastic book Elements of Programming Interviews.

The 0/1 Knapsack Problem (Demystifying Dynamic Programming)

Поделиться в:

Доступные форматы для скачивания:

Скачать видео mp4

  • Информация по загрузке:

Скачать аудио mp3

Похожие видео

0/1 Knapsack Problem Explained Visually

0/1 Knapsack Problem Explained Visually

4.5 0/1 Knapsack - Two Methods - Dynamic Programming

4.5 0/1 Knapsack - Two Methods - Dynamic Programming

Longest Common Subsequence (2 Strings) - Dynamic Programming & Competing Subproblems

Longest Common Subsequence (2 Strings) - Dynamic Programming & Competing Subproblems

Mastering Dynamic Programming - How to solve any interview problem (Part 1)

Mastering Dynamic Programming - How to solve any interview problem (Part 1)

Maximum Sum Rectangle In A 2D Matrix - Kadane's Algorithm Applications (Dynamic Programming)

Maximum Sum Rectangle In A 2D Matrix - Kadane's Algorithm Applications (Dynamic Programming)

The Knapsack Problem & Genetic Algorithms - Computerphile

The Knapsack Problem & Genetic Algorithms - Computerphile

0/1 Knapsack problem | Dynamic Programming

0/1 Knapsack problem | Dynamic Programming

The Change Making Problem - Fewest Coins To Make Change Dynamic Programming

The Change Making Problem - Fewest Coins To Make Change Dynamic Programming

Implement A Binary Heap - An Efficient Implementation of The Priority Queue ADT (Abstract Data Type)

Implement A Binary Heap - An Efficient Implementation of The Priority Queue ADT (Abstract Data Type)

The Backtracking Blueprint: The Legendary 3 Keys To Backtracking Algorithms

The Backtracking Blueprint: The Legendary 3 Keys To Backtracking Algorithms

© 2025 dtub. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: [email protected]