JS implements dynamic programming knapsack algorithm
Mar 22, 2018 pm 03:28 PMDuring the interview, I encountered a question about the backpack algorithm. It is slightly different from the traditional backpack. Given the capacity of the backpack and the weight of various items, the total mass of the items placed is required to be as close as possible to the capacity of the backpack and smaller than the backpack. capacity and the minimum number of items placed. This article mainly shares with you the dynamic programming backpack algorithm implemented in JS. I hope it can help you. function Backpack() {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
The core is to create a two-dimensional array to save the local optimal solution, and then slowly deduce it, and finally obtain the final optimal solution.
using using using using out out through off ’s ’ through out out through out outmb together out right out out out out out out outmb out out out out out out out out out out out out , -- 1-. The quality of the items in the line
2. New arr = arr[i-1][Remaining mass of backpack] + current item (use concat)
3. New arr and column j of the previous row Comparison of arr (if the initial conditions are different, you only need to change here)
4. Obtain arr
Related recommendations:
JavaScript Advanced Algorithm Dynamic programming example analysisphp algorithm learning dynamic programming
PHP dynamic programming to solve the 0-1 knapsack problem example analysis_PHP tutorial
The above is the detailed content of JS implements dynamic programming knapsack algorithm. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions

Explore the underlying principles and algorithm selection of the C++sort function

Improved detection algorithm: for target detection in high-resolution optical remote sensing images

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities

Simple JavaScript Tutorial: How to Get HTTP Status Code

PHP algorithm analysis: efficient method to find missing numbers in an array

Application of algorithms in the construction of 58 portrait platform
