{"id":32680,"date":"2023-02-01T02:31:50","date_gmt":"2023-01-31T21:01:50","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/"},"modified":"2023-02-01T02:31:50","modified_gmt":"2023-01-31T21:01:50","slug":"solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/","title":{"rendered":"[Solved] How can we develop an algorithm to solve a problem of DP [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-61264685\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"61264685\" data-parentid=\"61264309\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You can use DP to solve it. Here is a good website for learning DP: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.geeksforgeeks.org\/dynamic-programming\/\">https:\/\/www.geeksforgeeks.org\/dynamic-programming\/<\/a><\/p>\n<pre><code># For example X=3, Y=5, D=24. If we know solution for D=21 (24-3) and D=19 (24-5), then we know for D=24 the solution is min(D=21, D=19) +1.\n# And for D=19, we know it's min(D=16, D=14) +1. So all the way back to D=3 and D=5.\ndef sol(X,Y,D):\n\n    # dp list for storing the solution for each D.\n    # For inner list, index 0 represent the usage of X, index 1 represent the usage of Y.\n    dp = [[float('inf'), float('inf')] for _ in range(D+1)]\n\n    # Assume X &lt;= D and Y &lt;= D, it guarantees both X and Y can fit in D.\n    # for D=X, the solution is 1X, 0Y, so it's[1,0]\n    dp[X] = [1,0]\n    # for D=Y, the solution is 0X, 1Y, so it's[0,1]\n    dp[Y] = [0,1]\n\n    for i in range(min(X,Y)+1, D+1):\n\n        # If already has a solution, skip. \n        if dp[i][0] != float('inf'):\n            continue\n\n        # Current D= min(D-X, D-Y) + 1\n        if sum(dp[i-X]) &lt; sum(dp[i-Y]):\n            dp[i][0] = dp[i-X][0]+1\n            dp[i][1] = dp[i-X][1]\n        else:\n            dp[i][0] = dp[i-Y][0]\n            dp[i][1] = dp[i-Y][1]+1\n\n    # Search for a solution backward.\n    for i in range(len(dp)-1, -1, -1):\n        if dp[i][0] != float('inf') and dp[i][1] != float('inf'):\n            return (D-i, dp[i][0], dp[i][1])\n    return (D-i, 0, 0)\n\nprint(sol(3,5,24))\n<\/code><\/pre>\n<p>Here is a simplified question for understanding DP:<\/p>\n<pre><code>'''\nTo make it easier understand, let's simplify the question: we just want to know the minimium number of tables to get to D.\nFor example, X=3, Y=5, D=15. \n'''\ndef sol(X,Y,D):\n\n    # 1. We create a list of size D+1: dp = [None, None, None, ....], +1 just for easier calculation. Since the index starts at 0.\n    #    float('inf') represents infinity number, just for easier to use below. You will see it later.\n    dp = [float('inf') for _ in range(D+1)]\n\n    # 2. Our base case: when D=3, we know X=1,Y=0 is the solution, so only need 1 table to get to D=3.\n    #    Same for D=5: when D=5, we know X=0,Y=1 is the solution.\n    #    So set our base case: dp[3] = 1, dp[5] = 1\n    dp[X] = 1\n    dp[Y] = 1\n\n    # 4. Now the list is dp = [None, None, None, 1, None, 1, None, ....]\n    # 5. For D &lt; min(X, Y), that's for sure no solution. \n    #    So we start calculating solution for each D &gt; min(X,Y) (D=min(X,Y) is our base case)\n    for i in range(min(X,Y)+1, D+1):\n\n        # If already has a solution, skip. This is for preventing overwrite case such as Y can be formed by X.\n        # For example, X=3, Y=6, on dp[6] we don't want the solution from dp[6-3].\n        if dp[i] != float('inf'):\n            continue\n\n        # 7. Let's take example at D=8. The solution could D=3 + 1 table, or D=5 + 1 table.\n        #    Since we want the minimum, so our solution for D=8 is: min(D-3, D-5) + 1\n        #    Here is the reason we initiate value is float('inf') instead of None.\n        #    We don't have to check if it's None before calculate.\n        #    For example D=7, D-3 is None, D-5 is None. min(None, None) throws error, \n        #    we have to check if it's None or not before calculate. But if we use float('inf'),\n        #    min(float('inf'), float('inf')) + 1 is still float('inf').\n        dp[i] = min(dp[i-X], dp[i-Y]) + 1\n\n\n    # 8. Search for a smallest distance solution. For D=24, there is already a solution, so return dp[24]\n    #    But some D don't have solution. For example D=7 doesn't have a solution, so we check, D=6, D=5.. until\n    #    it finds a solution. And that's the smallest distance solution.\n    for i in range(len(dp)-1, -1, -1):\n        if dp[i] != float('inf'):\n            # first element is the distance, second is the number of tables.\n            return (D-i, dp[i])\n    return (D-i, 0)\n\nprint(sol(3,5,15))\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How can we develop an algorithm to solve a problem of DP [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You can use DP to solve it. Here is a good website for learning DP: https:\/\/www.geeksforgeeks.org\/dynamic-programming\/ # For example X=3, Y=5, D=24. If we know solution for D=21 (24-3) and D=19 (24-5), then we know for D=24 the solution is min(D=21, D=19) +1. # And for D=19, we know it&#8217;s min(D=16, D=14) +1. So &#8230; <a title=\"[Solved] How can we develop an algorithm to solve a problem of DP [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\" aria-label=\"More on [Solved] How can we develop an algorithm to solve a problem of DP [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[457,349],"class_list":["post-32680","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How can we develop an algorithm to solve a problem of DP [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How can we develop an algorithm to solve a problem of DP [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You can use DP to solve it. Here is a good website for learning DP: https:\/\/www.geeksforgeeks.org\/dynamic-programming\/ # For example X=3, Y=5, D=24. If we know solution for D=21 (24-3) and D=19 (24-5), then we know for D=24 the solution is min(D=21, D=19) +1. # And for D=19, we know it&#039;s min(D=16, D=14) +1. So ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-31T21:01:50+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How can we develop an algorithm to solve a problem of DP [closed]\",\"datePublished\":\"2023-01-31T21:01:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\"},\"wordCount\":58,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"algorithm\",\"python\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\",\"name\":\"[Solved] How can we develop an algorithm to solve a problem of DP [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-31T21:01:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How can we develop an algorithm to solve a problem of DP [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How can we develop an algorithm to solve a problem of DP [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How can we develop an algorithm to solve a problem of DP [closed] - JassWeb","og_description":"[ad_1] You can use DP to solve it. Here is a good website for learning DP: https:\/\/www.geeksforgeeks.org\/dynamic-programming\/ # For example X=3, Y=5, D=24. If we know solution for D=21 (24-3) and D=19 (24-5), then we know for D=24 the solution is min(D=21, D=19) +1. # And for D=19, we know it's min(D=16, D=14) +1. So ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/","og_site_name":"JassWeb","article_published_time":"2023-01-31T21:01:50+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How can we develop an algorithm to solve a problem of DP [closed]","datePublished":"2023-01-31T21:01:50+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/"},"wordCount":58,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","python"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/","name":"[Solved] How can we develop an algorithm to solve a problem of DP [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-31T21:01:50+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-we-develop-an-algorithm-to-solve-a-problem-of-dp-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How can we develop an algorithm to solve a problem of DP [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=32680"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32680\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=32680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=32680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=32680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}