{"id":34170,"date":"2024-11-01T09:25:04","date_gmt":"2024-11-01T09:25:04","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34170"},"modified":"2024-11-01T10:58:20","modified_gmt":"2024-11-01T10:58:20","slug":"c-coding-test-course-exploring-dynamic-programming-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34170\/","title":{"rendered":"C++ Coding Test Course, Exploring Dynamic Programming"},"content":{"rendered":"<p><body><\/p>\n<h2>1. What is Dynamic Programming?<\/h2>\n<p>Dynamic Programming (DP) is an algorithm design technique that solves complex problems by breaking them down into smaller subproblems and storing the results of already computed values to avoid redundant calculations. It is mainly used for optimization problems, reducing time complexity and providing efficient solutions.<\/p>\n<h3>1.1. Characteristics of Dynamic Programming<\/h3>\n<ul>\n<li><strong>Optimal Substructure:<\/strong> The optimal solution to the problem must include optimal solutions to its subproblems.<\/li>\n<li><strong>Overlapping Subproblems:<\/strong> Prevents the same subproblem from being solved multiple times.<\/li>\n<\/ul>\n<h2>2. Problem Introduction<\/h2>\n<h3>Problem: Maximum Subarray Sum<\/h3>\n<p>This problem involves finding the maximum sum of a contiguous subarray within an array of integers. For example, the maximum subarray sum of the array <code>{-2, 1, -3, 4, -1, 2, 1, -5, 4}<\/code> is <code>6<\/code>, which is the sum of the subarray <code>{4, -1, 2, 1}<\/code>.<\/p>\n<h3>Input<\/h3>\n<ul>\n<li>The first line contains the size of the array <code>N<\/code> (1 \u2264 N \u2264 100,000).<\/li>\n<li>The second line contains the elements of the array separated by spaces.<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>Print the maximum subarray sum.<\/p>\n<h2>3. Problem Solving Process<\/h2>\n<h3>3.1. Approach<\/h3>\n<p>To solve this problem using dynamic programming, the following steps can be taken:<\/p>\n<ol>\n<li><strong>Define the problem:<\/strong> Define <code>dp[i]<\/code> as the maximum subarray sum up to the <code>i<\/code>th element.<\/li>\n<li><strong>Establish the recurrence relation:<\/strong> The recurrence relation is <code>dp[i] = max(arr[i], dp[i-1] + arr[i])<\/code>. That is, decide whether to include the <code>i<\/code>th element or not.<\/li>\n<li><strong>Set initial values:<\/strong> Start with <code>dp[0] = arr[0]<\/code>.<\/li>\n<li><strong>Calculate the maximum:<\/strong> Iterate through all elements to update the maximum value.<\/li>\n<\/ol>\n<h3>3.2. C++ Code Implementation<\/h3>\n<pre>\n<code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nusing namespace std;\n\nint main() {\n    int N;\n    cin &gt;&gt; N;\n    vector<int> arr(N);\n    for(int i = 0; i &lt; N; i++) {\n        cin &gt;&gt; arr[i];\n    }\n\n    vector<int> dp(N);\n    dp[0] = arr[0];\n    int maxSum = dp[0];\n\n    for(int i = 1; i &lt; N; i++) {\n        dp[i] = max(arr[i], dp[i - 1] + arr[i]);\n        maxSum = max(maxSum, dp[i]);\n    }\n\n    cout &lt;&lt; maxSum &lt;&lt; endl;\n\n    return 0;\n}\n<\/int><\/int><\/code>\n    <\/pre>\n<h2>4. Code Explanation<\/h2>\n<p>The above code first takes the size of the array <code>N<\/code> and the elements of the array as input. Then, it declares a <code>dp<\/code> array to store the maximum subarray sum up to each index. After that, it updates the <code>dp<\/code> values inside a loop while finding the maximum value in the process.<\/p>\n<h3>4.1. Time Complexity<\/h3>\n<p>The time complexity of this algorithm is <code>O(N)<\/code>. It has linear time complexity because it iterates through the array once.<\/p>\n<h3>4.2. Space Complexity<\/h3>\n<p>The space complexity is also <code>O(N)<\/code>, as it stores up to <code>N<\/code> values. However, there is a method to find the maximum sum using just two variables without using the <code>dp<\/code> array.<\/p>\n<h2>5. Conclusion<\/h2>\n<p>Dynamic programming is a powerful technique for efficiently solving complex problems. The maximum subarray sum problem helps understand the basic concepts of dynamic programming and often appears in coding tests, making sufficient practice necessary.<\/p>\n<footer>\n<p>\u00a9 2023 C++ Coding Test Course<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is Dynamic Programming? Dynamic Programming (DP) is an algorithm design technique that solves complex problems by breaking them down into smaller subproblems and storing the results of already computed values to avoid redundant calculations. It is mainly used for optimization problems, reducing time complexity and providing efficient solutions. 1.1. Characteristics of Dynamic Programming &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34170\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Exploring Dynamic Programming&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34170","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Exploring Dynamic Programming - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/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:\/\/atmokpo.com\/w\/34170\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Exploring Dynamic Programming - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. What is Dynamic Programming? Dynamic Programming (DP) is an algorithm design technique that solves complex problems by breaking them down into smaller subproblems and storing the results of already computed values to avoid redundant calculations. It is mainly used for optimization problems, reducing time complexity and providing efficient solutions. 1.1. Characteristics of Dynamic Programming &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Exploring Dynamic Programming&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34170\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:20+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34170\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34170\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Exploring Dynamic Programming\",\"datePublished\":\"2024-11-01T09:25:04+00:00\",\"dateModified\":\"2024-11-01T10:58:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34170\/\"},\"wordCount\":367,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34170\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34170\/\",\"name\":\"C++ Coding Test Course, Exploring Dynamic Programming - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:04+00:00\",\"dateModified\":\"2024-11-01T10:58:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34170\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34170\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34170\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Exploring Dynamic Programming\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Exploring Dynamic Programming - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/34170\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Exploring Dynamic Programming - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. What is Dynamic Programming? Dynamic Programming (DP) is an algorithm design technique that solves complex problems by breaking them down into smaller subproblems and storing the results of already computed values to avoid redundant calculations. It is mainly used for optimization problems, reducing time complexity and providing efficient solutions. 1.1. Characteristics of Dynamic Programming &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Exploring Dynamic Programming\"","og_url":"https:\/\/atmokpo.com\/w\/34170\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:04+00:00","article_modified_time":"2024-11-01T10:58:20+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34170\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34170\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Exploring Dynamic Programming","datePublished":"2024-11-01T09:25:04+00:00","dateModified":"2024-11-01T10:58:20+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34170\/"},"wordCount":367,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34170\/","url":"https:\/\/atmokpo.com\/w\/34170\/","name":"C++ Coding Test Course, Exploring Dynamic Programming - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:04+00:00","dateModified":"2024-11-01T10:58:20+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34170\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34170\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34170\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Exploring Dynamic Programming"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34170"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34170\/revisions"}],"predecessor-version":[{"id":34171,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34170\/revisions\/34171"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}