{"id":34172,"date":"2024-11-01T09:25:05","date_gmt":"2024-11-01T09:25:05","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34172"},"modified":"2024-11-01T10:58:19","modified_gmt":"2024-11-01T10:58:19","slug":"c-coding-test-course-finding-the-minimum-number-of-coins-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34172\/","title":{"rendered":"C++ Coding Test Course, Finding the Minimum Number of Coins"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today we will discuss one of the frequently asked questions in C++ coding tests, which is &#8216;Finding the Minimum Number of Coins.&#8217; This problem is a basic algorithm problem that determines the minimum number of coins needed to make a given amount. We will analyze this problem step by step and solve it through implementation.<\/p>\n<h2>Problem Description<\/h2>\n<p>Given the types of coins and a target amount, find out the minimum number of coins required to make that amount. Each coin is assumed to be available in infinite supply.<\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>The first line contains the number of coin types <code>N<\/code> (1 \u2264 N \u2264 100).<\/li>\n<li>The second line contains <code>N<\/code> integers representing the value of each coin.<\/li>\n<li>The third line contains the target amount <code>M<\/code> (1 \u2264 M \u2264 10,000).<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<ul>\n<li>Output the minimum number of coins needed to make the target amount. If it is not possible to make the target amount, output <code>-1<\/code>.<\/li>\n<\/ul>\n<h2>Example<\/h2>\n<pre>\nExample Input:\n3\n1 3 4\n6\n\nExample Output:\n2\n<\/pre>\n<p>In the above example, there are 3 types of coins with values of 1, 3, and 4. To make the target amount of 6, using the coins 3 and 3 will result in a total of 2 coins.<\/p>\n<h2>Approach to Solve the Problem<\/h2>\n<p>This problem can be solved using Dynamic Programming or Greedy Algorithm. However, when there are multiple types of coins, using Dynamic Programming provides a more general solution. Below is the approach to solving the problem using Dynamic Programming.<\/p>\n<h3>Basic Concept of Dynamic Programming<\/h3>\n<p>Dynamic Programming (DP) is a method of solving large problems by dividing them into smaller ones. In this problem, we can reduce redundant calculations while finding the minimum number of coins required to make each amount to reach the target amount. We will use a DP array to store the minimum number of coins needed for each amount.<\/p>\n<h3>Definition of DP Array<\/h3>\n<p>We define <code>dp[i]<\/code> as the minimum number of coins needed to make the target amount <code>i<\/code>. We set the initial value <code>dp[0] = 0<\/code> and initialize the other amounts to <code>INT_MAX<\/code> (infinity).<\/p>\n<h2>Solution<\/h2>\n<p>Now, let&#8217;s implement this based on the theory. The code is as follows:<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n#include &lt;climits&gt;\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin &gt;&gt; N; \/\/ Input the number of coin types\n    vector<int> coins(N);\n    for (int i = 0; i &lt; N; ++i) {\n        cin &gt;&gt; coins[i]; \/\/ Input the value of each coin\n    }\n    cin &gt;&gt; M; \/\/ Input the target amount\n\n    vector<int> dp(M + 1, INT_MAX); \/\/ Initialize DP array\n    dp[0] = 0; \/\/ The number of coins needed to make 0 amount is 0\n\n    \/\/ Fill the DP array\n    for (int i = 1; i &lt;= M; ++i) {\n        for (int j = 0; j &lt; N; ++j) {\n            if (coins[j] &lt;= i) {\n                dp[i] = min(dp[i], dp[i - coins[j]] + 1);\n            }\n        }\n    }\n\n    \/\/ Output the result\n    if (dp[M] == INT_MAX) {\n        cout &lt;&lt; -1 &lt;&lt; endl; \/\/ If it cannot be made\n    } else {\n        cout &lt;&lt; dp[M] &lt;&lt; endl; \/\/ Output the minimum number of coins\n    }\n\n    return 0;\n}\n<\/int><\/int><\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<ul>\n<li>First, include the necessary libraries and input the number of coin types and the target amount.<\/li>\n<li>Define a vector <code>coins<\/code> to store the values of the coins and an array <code>dp<\/code> to store the number of coins.<\/li>\n<li>Use a nested loop to fill the <code>dp<\/code> array. The outer loop iterates over the target amount, and the inner loop iterates over the types of coins to calculate the minimum number of coins.<\/li>\n<li>In the end, if the value of <code>dp[M]<\/code> is <code>INT_MAX<\/code>, it means the amount M cannot be made, so output <code>-1<\/code>. Otherwise, output the minimum number of coins.<\/li>\n<\/ul>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of this algorithm is <code>O(N*M)<\/code>. Here, <code>N<\/code> is the number of coin types, and <code>M<\/code> is the target amount. The space complexity is <code>O(M)<\/code>, which is the space needed to maintain the DP array.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we learned how to solve the &#8216;Finding the Minimum Number of Coins&#8217; problem frequently occurring in C++ coding tests using Dynamic Programming. I hope this problem helped you understand the basic concepts of Dynamic Programming and how to use the DP array to solve problems. Dynamic Programming is an essential technique for solving various problems, so practice adequately to improve your understanding! I encourage you to also work on other algorithm problems to enhance your skills.<\/p>\n<p>Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today we will discuss one of the frequently asked questions in C++ coding tests, which is &#8216;Finding the Minimum Number of Coins.&#8217; This problem is a basic algorithm problem that determines the minimum number of coins needed to make a given amount. We will analyze this problem step by step and solve it through &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34172\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding the Minimum Number of Coins&#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-34172","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, Finding the Minimum Number of Coins - \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\/34172\/\" \/>\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, Finding the Minimum Number of Coins - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today we will discuss one of the frequently asked questions in C++ coding tests, which is &#8216;Finding the Minimum Number of Coins.&#8217; This problem is a basic algorithm problem that determines the minimum number of coins needed to make a given amount. We will analyze this problem step by step and solve it through &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding the Minimum Number of Coins&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34172\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:19+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34172\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34172\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding the Minimum Number of Coins\",\"datePublished\":\"2024-11-01T09:25:05+00:00\",\"dateModified\":\"2024-11-01T10:58:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34172\/\"},\"wordCount\":549,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34172\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34172\/\",\"name\":\"C++ Coding Test Course, Finding the Minimum Number of Coins - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:05+00:00\",\"dateModified\":\"2024-11-01T10:58:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34172\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34172\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34172\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Finding the Minimum Number of Coins\"}]},{\"@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, Finding the Minimum Number of Coins - \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\/34172\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding the Minimum Number of Coins - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today we will discuss one of the frequently asked questions in C++ coding tests, which is &#8216;Finding the Minimum Number of Coins.&#8217; This problem is a basic algorithm problem that determines the minimum number of coins needed to make a given amount. We will analyze this problem step by step and solve it through &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding the Minimum Number of Coins\"","og_url":"https:\/\/atmokpo.com\/w\/34172\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:05+00:00","article_modified_time":"2024-11-01T10:58:19+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34172\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34172\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding the Minimum Number of Coins","datePublished":"2024-11-01T09:25:05+00:00","dateModified":"2024-11-01T10:58:19+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34172\/"},"wordCount":549,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34172\/","url":"https:\/\/atmokpo.com\/w\/34172\/","name":"C++ Coding Test Course, Finding the Minimum Number of Coins - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:05+00:00","dateModified":"2024-11-01T10:58:19+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34172\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34172\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34172\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Finding the Minimum Number of Coins"}]},{"@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\/34172","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=34172"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34172\/revisions"}],"predecessor-version":[{"id":34173,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34172\/revisions\/34173"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}