{"id":29690,"date":"2023-11-13T13:38:56","date_gmt":"2023-11-13T08:08:56","guid":{"rendered":"https:\/\/www.javaassignmenthelp.com\/blog\/?p=29690"},"modified":"2025-02-25T05:15:47","modified_gmt":"2025-02-25T10:45:47","slug":"dynamic-programming-examples","status":"publish","type":"post","link":"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/","title":{"rendered":"10 Best Dynamic Programming Examples: Unveiling Practical Implementations"},"content":{"rendered":"\n<p>Dive into the practical world of dynamic programming examples across diverse fields. Explore the optimization wonders, algorithmic strategies, and problem-solving brilliance that dynamic programming brings to industries like computer science, finance, engineering, and more.&#8221;<\/p>\n\n\n\n<p>Hey there, curious minds! Ever heard of dynamic programming? It&#8217;s not just a tech buzzword; think of it as the superhero of algorithms, swooping in to solve some seriously tricky puzzles. <\/p>\n\n\n\n<p>We&#8217;re diving into the exciting world of dynamic programming examples, where algorithms get downright creative. <\/p>\n\n\n\n<p>From plotting the quickest road trips to deciphering the secrets hidden in DNA, dynamic programming is the behind-the-scenes rockstar making it all happen. <\/p>\n\n\n\n<p>So, get ready for a rollercoaster ride through real-world applications that&#8217;ll show you just how dynamic programming steals the spotlight. Let&#8217;s jump in!&nbsp;<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_68_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Overview<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#benefits-of-dynamic-programming\" title=\"Benefits of Dynamic Programming\">Benefits of Dynamic Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#dynamic-programming-examples\" title=\"Dynamic Programming Examples\">Dynamic Programming Examples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#how-is-dynamic-programming-used-in-real-life\" title=\"How is dynamic programming used in real life?\">How is dynamic programming used in real life?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#which-of-the-following-are-examples-of-dynamic-programming\" title=\"Which of the following are examples of dynamic programming?\">Which of the following are examples of dynamic programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#what-are-three-applications-of-dynamic-programming\" title=\"What are three applications of dynamic programming?\">What are three applications of dynamic programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#frequetly-asked-questions\" title=\"Frequetly Asked Questions\">Frequetly Asked Questions<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"benefits-of-dynamic-programming\"><\/span>Benefits of Dynamic Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Dynamic programming is like the multitasking maestro of problem-solving, and it comes with a bunch of perks that make it a real rockstar in the algorithm world. Check out these cool benefits:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Optimal Awesomeness<\/h3>\n\n\n\n<p>Dynamic programming doesn&#8217;t settle for anything less than optimal solutions. It breaks down problems into bite-sized chunks, figures out the best way to solve each piece, and voila\u2014optimal solution achieved!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Time Magic<\/h3>\n\n\n\n<p>Say goodbye to waiting around. Dynamic programming is all about efficiency. By remembering solutions to subproblems, it skips the boring parts and zooms straight to the good stuff. Time saved, problem conquered.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Jack-of-All-Trades<\/h3>\n\n\n\n<p>This technique is like the Swiss Army knife of problem-solving. Whether you&#8217;re tackling route planning, decoding DNA in bioinformatics, or divvying up resources, dynamic programming is up for the challenge. Versatility at its finest!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Memory Whisperer<\/h3>\n\n\n\n<p>Dynamic programming isn&#8217;t a memory hog. By storing solutions cleverly, it keeps things tidy. Perfect for dealing with big datasets or problems that need some serious brainpower.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Complex Problems, Meet Your Match<\/h3>\n\n\n\n<p>Got a problem that&#8217;s like a puzzle wrapped in an enigma? Dynamic programming is your go-to guru for breaking down complex issues into manageable chunks. It&#8217;s like a problem-solving superhero.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scale Up or Down<\/h3>\n\n\n\n<p>Dynamic programming is a problem-solving chameleon. Whether you&#8217;ve got a teeny tiny problem or a massive one, it scales like a pro. Small or big, it&#8217;s got your back.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Global &amp; Local Swagger<\/h3>\n\n\n\n<p>Dynamic programming isn&#8217;t picky. It&#8217;s great at finding the best solution overall (that&#8217;s the global optimization bit) and also excels at optimizing in specific areas (hello, local optimization!).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Memory Lane Made Easy<\/h3>\n\n\n\n<p>Don&#8217;t let the word &#8220;dynamic&#8221; scare you. Implementing dynamic programming is often simpler than deciphering IKEA instructions. It&#8217;s like a strategy game that&#8217;s surprisingly easy to play.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Problem-Solving Wizardry<\/h3>\n\n\n\n<p>Whether it&#8217;s computer science, finance, or biology, dynamic programming is the wizard waving its wand and solving problems like magic. No big deal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Strategic Decision Whiz<\/h3>\n\n\n\n<p>Dynamic programming isn&#8217;t just a problem-solver; it&#8217;s a decision-making guru. Perfect for those moments when you need to make the smartest move with your resources.<\/p>\n\n\n\n<p>So there you have it\u2014dynamic programming isn&#8217;t just a technique; it&#8217;s the MVP in the world of problem-solving, making the tough stuff look like a walk in the park.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"dynamic-programming-examples\"><\/span><strong>Dynamic Programming Examples<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Check out dynamic programming examples:-<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fibonacci Series<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Proble<\/h4>\n\n\n\n<p>Generate the Fibonacci series up to a given term.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Apply dynamic programming to store previously calculated Fibonacci numbers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def fibonacci(n):\n\n\u00a0dp = [0] * (n + 1)\n\n\u00a0dp[1] = 1\n\n\u00a0for i in range(2, n + 1):\n\n\u00a0dp[i] = dp[i - 1] + dp[i - 2]\n\n\u00a0return dp\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">fibonacci<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">n<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> (n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">, n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\"><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example<\/strong><\/h4>\n\n\n\n<p>Input: n = 5<\/p>\n\n\n\n<p>Output: [0, 1, 1, 2, 3, 5]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Coin Change Problem<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Determine the minimum number of coins needed to make up a given amount.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Use dynamic programming to calculate the minimum number of coins for each amount.<\/p>\n\n\n\n<p>Code:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def coin_change(coins, amount):\n\n\u00a0dp = [float('inf')] * (amount + 1)\n\n\u00a0dp[0] = 0\n\n\u00a0for coin in coins:\n\n\u00a0for i in range(coin, amount + 1):\n\n\u00a0dp[i] = min(dp[i], dp[i - coin] + 1)\n\n\u00a0return dp[amount] if dp[amount] != float('inf') else -1\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">coin_change<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">coins<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">amount<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [<\/span><span style=\"color: #97E1F1; font-style: italic\">float<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">inf<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">)] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> (amount <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> coin <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> coins:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(coin, amount <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">min<\/span><span style=\"color: #F6F6F4\">(dp[i], dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> coin] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp[amount] <\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> dp[amount] <\/span><span style=\"color: #F286C4\">!=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">float<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">inf<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">else<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">1<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: coins = [1, 2, 5], amount = 11<\/p>\n\n\n\n<p>Output: 3 (11 = 5 + 5 + 1)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Longest Increasing Subsequence<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Find the length of the longest increasing subsequence in an array.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Utilize dynamic programming to track the length of increasing subsequences.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def length_of_lis(nums):\n\n\u00a0dp = [1] * len(nums)\n\n\u00a0for i in range(len(nums)):\n\n\u00a0for j in range(i):\n\n\u00a0if nums[i] &gt; nums[j]:\n\n\u00a0dp[i] = max(dp[i], dp[j] + 1)\n\n\u00a0return max(dp)\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">length_of_lis<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">nums<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(nums)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(nums)):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> j <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(i):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> nums[i] <\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\"> nums[j]:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">max<\/span><span style=\"color: #F6F6F4\">(dp[i], dp[j] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">max<\/span><span style=\"color: #F6F6F4\">(dp)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: nums = [10, 9, 2, 5, 3, 7, 101, 18]<\/p>\n\n\n\n<p>Output: 4 (The LIS is [2, 5, 7, 101])<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Knapsack Problem<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Given items with weights and values, determine the maximum value that can be accommodated in a knapsack of a given capacity.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Apply dynamic programming to calculate the maximum value for each combination of items and capacities.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def knapsack(weights, values, capacity):\n\n\u00a0n = len(weights)\n\n\u00a0dp = [[0] * (capacity + 1) for _ in range(n + 1)]\n\n\u00a0for i in range(1, n + 1):\n\n\u00a0for w in range(capacity + 1):\n\n\u00a0if weights[i - 1] &lt;= w:\n\n\u00a0dp[i][w] = max(dp[i - 1][w], values[i - 1] + dp[i - 1][w - weights[i - 1]])\n\n\u00a0else:\n\n\u00a0dp[i][w] = dp[i - 1][w]\n\n\u00a0return dp[n][capacity]\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">knapsack<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">weights<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">values<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">capacity<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0n <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(weights)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [[<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> (capacity <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> _ <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> w <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(capacity <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> weights[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">&lt;=<\/span><span style=\"color: #F6F6F4\"> w:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][w] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">max<\/span><span style=\"color: #F6F6F4\">(dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][w], values[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][w <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> weights[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">else<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][w] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][w]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp[n][capacity]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: weights = [2, 1, 3], values = [4, 2, 3], capacity = 4<\/p>\n\n\n\n<p>Output: 7 (Select items 1 and 3 for a total weight of 4 and value of 7)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Edit Distance<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Find the minimum number of operations required to convert one string into another (insertion, deletion, or substitution).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Utilize dynamic programming to compute the minimum edit distance.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def min_distance(word1, word2):\n\n\u00a0m, n = len(word1), len(word2)\n\n\u00a0dp = [[0] * (n + 1) for _ in range(m + 1)]\n\n\u00a0for i in range(m + 1):\n\n\u00a0for j in range(n + 1):\n\n\u00a0if i == 0:\n\n\u00a0dp[i][j] = j\n\n\u00a0elif j == 0:\n\n\u00a0dp[i][j] = i\n\n\u00a0elif word1[i - 1] == word2[j - 1]:\n\n\u00a0dp[i][j] = dp[i - 1][j - 1]\n\n\u00a0else:\n\n\u00a0dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])\n\n\u00a0return dp[m][n]\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">min_distance<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">word1<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">word2<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0m, n <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(word1), <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(word2)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [[<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> (n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> _ <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(m <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(m <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> j <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">==<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> j<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">elif<\/span><span style=\"color: #F6F6F4\"> j <\/span><span style=\"color: #F286C4\">==<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> i<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">elif<\/span><span style=\"color: #F6F6F4\"> word1[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">==<\/span><span style=\"color: #F6F6F4\"> word2[j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">else<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">min<\/span><span style=\"color: #F6F6F4\">(dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j], dp[i][j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">], dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp[m][n]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: word1 = &#8220;intention&#8221;, word2 = &#8220;execution&#8221;<\/p>\n\n\n\n<p>Output: 5 (Convert &#8220;intention&#8221; to &#8220;execution&#8221; with 5 operations)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Matrix Chain Multiplication<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Determine the most efficient way to multiply a given sequence of matrices.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Utilize dynamic programming to find the optimal parenthesization.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def matrix_chain_order(p):\n\n\u00a0n = len(p) - 1\n\n\u00a0dp = [[0] * n for _ in range(n)]\n\n\u00a0for l in range(2, n + 1):\n\n\u00a0for i in range(n - l + 1):\n\n\u00a0j = i + l - 1\n\n\u00a0dp[i][j] = float('inf')\n\n\u00a0for k in range(i, j):\n\n\u00a0cost = dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1]\n\n\u00a0dp[i][j] = min(dp[i][j], cost)\n\n\u00a0return dp[0][n - 1]\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">matrix_chain_order<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">p<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0n <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(p) <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [[<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> n <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> _ <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(n)]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> l <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">, n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(n <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> l <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0j <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> l <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">float<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">inf<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> k <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(i, j):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0cost <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i][k] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> dp[k <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> p[i] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> p[k <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> p[j <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">min<\/span><span style=\"color: #F6F6F4\">(dp[i][j], cost)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp[<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">][n <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: p = [10, 20, 30, 40, 30]<\/p>\n\n\n\n<p>Output: 30000 (Optimal parenthesization: ((A1(A2A3))((A4A5)))<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Largest Sum Contiguous Subarray (Kadane&#8217;s Algorithm)<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Find the contiguous subarray with the largest sum.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Apply Kadane&#8217;s Algorithm using dynamic programming.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def max_subarray_sum(nums):\n\n\u00a0max_sum = float('-inf')\n\n\u00a0current_sum = 0\n\n\u00a0for num in nums:\n\n\u00a0current_sum = max(num, current_sum + num)\n\n\u00a0max_sum = max(max_sum, current_sum)\n\n\u00a0return max_sum\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">max_subarray_sum<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">nums<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0max_sum <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">float<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">-inf<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0current_sum <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> num <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> nums:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0current_sum <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">max<\/span><span style=\"color: #F6F6F4\">(num, current_sum <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> num)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0max_sum <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">max<\/span><span style=\"color: #F6F6F4\">(max_sum, current_sum)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> max_sum<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]<\/p>\n\n\n\n<p>Output: 6 (The subarray [4, -1, 2, 1] has the largest sum)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Longest Common Subsequence<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Find the length of the longest subsequence that appears in two given sequences.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Utilize dynamic programming to calculate the length of the longest common subsequence.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def longest_common_subsequence(text1, text2):\n\n\u00a0m, n = len(text1), len(text2)\n\n\u00a0dp = [[0] * (n + 1) for _ in range(m + 1)]\n\n\u00a0for i in range(1, m + 1):\n\n\u00a0for j in range(1, n + 1):\n\n\u00a0if text1[i - 1] == text2[j - 1]:\n\n\u00a0dp[i][j] = dp[i - 1][j - 1] + 1\n\n\u00a0else:\n\n\u00a0dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])\n\n\u00a0return dp[m][n]\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">longest_common_subsequence<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">text1<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">text2<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0m, n <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(text1), <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(text2)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [[<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> (n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> _ <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(m <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, m <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> j <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> text1[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">==<\/span><span style=\"color: #F6F6F4\"> text2[j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">else<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">max<\/span><span style=\"color: #F6F6F4\">(dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j], dp[i][j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp[m][n]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: text1 = &#8220;abcde&#8221;, text2 = &#8220;ace&#8221;<\/p>\n\n\n\n<p>Output: 3 (The longest common subsequence is &#8220;ace&#8221;)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Subset Sum Problem:<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Determine if there exists a subset of a given set with a sum equal to a given target.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Utilize dynamic programming to check the existence of a subset with the target sum.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def subset_sum(nums, target):\n\n\u00a0n = len(nums)\n\n\u00a0dp = [[False] * (target + 1) for _ in range(n + 1)]\n\n\u00a0for i in range(n + 1):\n\n\u00a0dp[i][0] = True\n\n\u00a0for i in range(1, n + 1):\n\n\u00a0for j in range(1, target + 1):\n\n\u00a0if nums[i - 1] &lt;= j:\n\n\u00a0dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]]\n\n\u00a0else:\n\n\u00a0dp[i][j] = dp[i - 1][j]\n\n\u00a0return dp[n][target]\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">subset_sum<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">nums<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">target<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0n <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(nums)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [[<\/span><span style=\"color: #BF9EEE\">False<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> (target <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> _ <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">True<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> j <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, target <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> nums[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">&lt;=<\/span><span style=\"color: #F6F6F4\"> j:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j] <\/span><span style=\"color: #F286C4\">or<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> nums[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">else<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][j] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][j]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp[n][target]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: nums = [3, 34, 4, 12, 5, 2], target = 9<\/p>\n\n\n\n<p>Output: True (Subset [3, 4, 2] has a sum of 9)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">0\/1 Knapsack Problem<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Given items with weights and values, determine the maximum value that can be accommodated in a knapsack of a given capacity, where each item can be selected at most once.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Apply dynamic programming to calculate the maximum value for each combination of items and capacities.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def knapsack_01(weights, values, capacity):\n\n\u00a0n = len(weights)\n\n\u00a0dp = [[0] * (capacity + 1) for _ in range(n + 1)]\n\n\u00a0for i in range(1, n + 1):\n\n\u00a0for w in range(capacity + 1):\n\n\u00a0if weights[i - 1] &lt;= w:\n\n\u00a0dp[i][w] = max(dp[i - 1][w], values[i - 1] + dp[i - 1][w - weights[i - 1]])\n\n\u00a0else:\n\n\u00a0dp[i][w] = dp[i - 1][w]\n\n\u00a0return dp[n][capacity]\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">knapsack_01<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">weights<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">values<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">capacity<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0n <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(weights)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> [[<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> (capacity <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> _ <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, n <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> w <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(capacity <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> weights[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">&lt;=<\/span><span style=\"color: #F6F6F4\"> w:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][w] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">max<\/span><span style=\"color: #F6F6F4\">(dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][w], values[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">] <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][w <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> weights[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">]])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">else<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0dp[i][w] <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dp[i <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">][w]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0<\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> dp[n][capacity]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>Input: weights = [2, 1, 3], values = [4, 2, 3], capacity = 4<\/p>\n\n\n\n<p>Output: 7 (Select items 1 and 3 for a total weight of 4 and value of 7)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Also Read<\/strong>: <a href=\"https:\/\/www.javaassignmenthelp.com\/blog\/math-project-ideas\/\" data-type=\"post\" data-id=\"29609\">60+ Innovative Math Project Ideas: Numbers Unleashed<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"how-is-dynamic-programming-used-in-real-life\"><\/span>How is dynamic programming used in real life?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Dynamic programming stands out as a potent optimization technique with diverse applications in the real world, spanning fields like computer science, economics, finance, and engineering.<\/p>\n\n\n\n<p>Illustrative instances of dynamic programming in practical scenarios include:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Route Optimization<\/h3>\n\n\n\n<p>GPS devices leverage dynamic programming to determine the most efficient route between two locations, factoring in variables such as traffic conditions, road closures, and speed limits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Speech Recognition<\/h3>\n\n\n\n<p>Speech recognition software employs dynamic programming to transcribe spoken words into text. <\/p>\n\n\n\n<p>The algorithm considers acoustic nuances, linguistic <a href=\"https:\/\/en.wikipedia.org\/wiki\/Grammar\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Grammar\" target=\"_blank\" rel=\"noopener\">grammar<\/a>, and contextual elements within the conversation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Image Processing<\/h3>\n\n\n\n<p>In image processing, dynamic programming finds use in tasks like noise reduction, edge enhancement, and image segmentation. <\/p>\n\n\n\n<p>It takes into account pixel values and spatial relationships to achieve these enhancements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Financial Modeling<\/h3>\n\n\n\n<p>Dynamic programming aids financial modeling by assessing investment risks and optimizing portfolios. <\/p>\n\n\n\n<p>Historical returns, market volatility, and investor risk tolerance are among the factors considered by the algorithm.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Resource Allocation<\/h3>\n\n\n\n<p>Dynamic programming optimizes the allocation of scarce resources, such as water, electricity, and bandwidth. <\/p>\n\n\n\n<p>The algorithm factors in resource demand, cost, and capacity to enhance efficiency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Game Theory<\/h3>\n\n\n\n<p>In game theory, dynamic programming analyzes optimal strategies for games like chess, poker, and Go. <\/p>\n\n\n\n<p>The algorithm evaluates potential moves for each player and the associated payoffs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scheduling<\/h3>\n\n\n\n<p>Dynamic programming plays a key role in scheduling by optimizing resource utilization, be it machines, workers, or transportation. <\/p>\n\n\n\n<p>Task specifics, task dependencies, and available resources are considered in the algorithm.<\/p>\n\n\n\n<p>These examples merely scratch the surface of dynamic programming&#8217;s real-world applications. <\/p>\n\n\n\n<p>Its adaptability and effectiveness make it a go-to solution for addressing complex optimization challenges in various domains.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"which-of-the-following-are-examples-of-dynamic-programming\"><\/span>Which of the following are examples of dynamic programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Dynamic programming is an algorithmic approach that tackles complex problems by breaking them into smaller, more manageable subproblems. <\/p>\n\n\n\n<p>The key innovation lies in storing solutions to these subproblems, preventing redundant computations. <\/p>\n\n\n\n<p>This methodology is especially effective for problems featuring overlapping subproblems, where solving the same subproblem occurs multiple times in the larger problem-solving process.<\/p>\n\n\n\n<p>Here are illustrative examples of dynamic programming problems:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fibonacci Sequence<\/h3>\n\n\n\n<p>A quintessential dynamic programming example, the Fibonacci sequence generates a series of numbers wherein each number is the sum of its two predecessors, commencing with 0 and 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Longest Common Subsequence (LCS) Problem<\/h3>\n\n\n\n<p>This problem aims to identify the lengthiest sequence of characters appearing in the same order in two given strings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Knapsack Problem<\/h3>\n\n\n\n<p>Tasked with determining the optimal subset of items to fit into a knapsack with a specified weight limit, maximizing the total value of the chosen items.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Edit Distance Problem<\/h3>\n\n\n\n<p>This challenge involves finding the minimum number of edits\u2014insertions, deletions, or substitutions\u2014needed to transform one string into another.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Traveling Salesman Problem (TSP)<\/h3>\n\n\n\n<p>The TSP seeks the shortest route traversing a set of cities, returning to the starting city while visiting each city exactly once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Optimal Binary Search Tree (OBST)<\/h3>\n\n\n\n<p>Identifying the binary search tree that minimizes the weighted average search time, where node weights denote the frequency of associated keys.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Subset Sum Problem<\/h3>\n\n\n\n<p>Determining whether a given set of integers contains a subset that sums up to a specified target sum.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Partition Problem<\/h3>\n\n\n\n<p>This problem involves ascertaining if a given set of integers can be partitioned into two subsets with equal sums.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Coin Changing Problem<\/h3>\n\n\n\n<p>The objective is to find the minimum number of coins required to make change for a given amount of money.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Bellman-Ford Algorithm<\/h3>\n\n\n\n<p>Used to uncover the shortest path from a source node to all other nodes in a graph, accommodating negative-weight edges.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dijkstra&#8217;s Algorithm<\/h3>\n\n\n\n<p>This algorithm also identifies the shortest path from a source node to all other nodes in a graph, with efficiency for graphs lacking negative-weight edges.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Floyd-Warshall Algorithm<\/h3>\n\n\n\n<p>Employed to determine the shortest path between all pairs of nodes in a graph, even in the presence of negative-weight edges.<\/p>\n\n\n\n<p>These examples merely scratch the surface of the breadth of dynamic programming problems. <\/p>\n\n\n\n<p>Its versatility and efficiency make dynamic programming an invaluable tool for solving a diverse array of optimization challenges.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"what-are-three-applications-of-dynamic-programming\"><\/span>What are three applications of dynamic programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Dynamic programming stands out as a robust optimization technique with diverse applications spanning computer science, finance, engineering, and biology. <\/p>\n\n\n\n<p>Here are three distinct examples showcasing how dynamic programming is harnessed:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Route Planning<\/h3>\n\n\n\n<p>Dynamic programming proves invaluable in determining the shortest or most efficient route between two points. This involves considering variables like traffic congestion, road conditions, and travel time. <\/p>\n\n\n\n<p>Its applications extend to planning road trips, optimizing delivery routes, and designing comprehensive transportation networks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sequence Alignment<\/h3>\n\n\n\n<p>In bioinformatics, dynamic programming plays a pivotal role in aligning biological sequences, such as DNA or protein sequences. <\/p>\n\n\n\n<p>This application is critical for tasks ranging from identifying homologous sequences to analyzing genetic variation and understanding protein structure and function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Resource Allocation<\/h3>\n\n\n\n<p>Dynamic programming is adept at optimizing the allocation of various resources, be it money, time, or energy, to attain specific goals. <\/p>\n\n\n\n<p>Examples include optimizing investment portfolios, scheduling tasks within a project, or efficiently distributing energy resources in a power grid.<\/p>\n\n\n\n<p>These instances merely scratch the surface of dynamic programming&#8217;s myriad applications. <\/p>\n\n\n\n<p>As a versatile optimization technique, it has permeated diverse fields, offering effective solutions to complex problems and aiding in making efficient decisions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In a nutshell, dynamic programming isn&#8217;t just a fancy algorithm\u2014it&#8217;s the unsung hero behind some pretty cool stuff. <\/p>\n\n\n\n<p>Whether it&#8217;s plotting the fastest road trip, deciphering the secrets of DNA, or figuring out the perfect balance of resources, dynamic programming is the behind-the-scenes wizard making it all happen.<\/p>\n\n\n\n<p>So, the next time you&#8217;re navigating traffic or marveling at breakthroughs in bioinformatics, remember that dynamic programming is the unsung superstar, turning complex puzzles into solvable pieces. <\/p>\n\n\n\n<p>As technology evolves, this powerhouse algorithm will likely keep surprising us with new tricks up its sleeve. Cheers to dynamic programming for making the complex seem downright doable!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"frequetly-asked-questions\"><\/span>Frequetly Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1699862351055\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is dynamic programming, and why is it important?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Dynamic programming is a problem-solving technique that simplifies complex problems by breaking them into more manageable subproblems. It is important because it enhances efficiency and optimizes solutions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699862355566\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How does dynamic programming differ from traditional approaches?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Dynamic programming differs by storing and reusing intermediate results, preventing redundant computations and significantly reducing time complexity.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699862360463\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can dynamic programming be applied to any problem?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>While dynamic programming is a powerful technique, it is most effective for problems with overlapping subproblems and optimal substructure.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699862365324\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Are there real-world applications of dynamic programming?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Absolutely! Dynamic programming finds applications in various fields, including finance, biology, and artificial intelligence, to solve optimization problems.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699862369836\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Is dynamic programming suitable for beginners in programming?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, beginners can grasp the fundamentals of dynamic programming by starting with simple examples and gradually tackling more complex problems.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Dive into the practical world of dynamic programming examples across diverse fields. Explore the optimization wonders, algorithmic strategies, and problem-solving &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"10 Best Dynamic Programming Examples: Unveiling Practical Implementations\" class=\"read-more button\" href=\"https:\/\/www.javaassignmenthelp.com\/blog\/dynamic-programming-examples\/#more-29690\" aria-label=\"Read more about 10 Best Dynamic Programming Examples: Unveiling Practical Implementations\">Read more<\/a><\/p>\n","protected":false},"author":34,"featured_media":29693,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-29690","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-education"],"_links":{"self":[{"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/posts\/29690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/comments?post=29690"}],"version-history":[{"count":3,"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/posts\/29690\/revisions"}],"predecessor-version":[{"id":29694,"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/posts\/29690\/revisions\/29694"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/media\/29693"}],"wp:attachment":[{"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/media?parent=29690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/categories?post=29690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javaassignmenthelp.com\/blog\/wp-json\/wp\/v2\/tags?post=29690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}