{"id":34126,"date":"2024-11-01T09:24:29","date_gmt":"2024-11-01T09:24:29","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34126"},"modified":"2024-11-01T10:58:30","modified_gmt":"2024-11-01T10:58:30","slug":"c-coding-test-course-finding-the-largest-square-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34126\/","title":{"rendered":"C++ Coding Test Course, Finding the Largest Square"},"content":{"rendered":"<div class=\"post\">\n<h2>Problem Description<\/h2>\n<p>\n        This is the problem of finding the largest square composed of 1&#8217;s in a given binary matrix. Each element of the matrix is either 0 or 1, and 1 can be included as part of the square.<br \/>\n        Determining the size of the square is based on the edge values of each 1, and thus, we will use a Dynamic Programming approach to solve the problem.\n    <\/p>\n<h2>Input Format<\/h2>\n<p>\n        Given the size of the matrix n x m, the input is provided in the following format.\n    <\/p>\n<pre>\n    [\n        ['1', '0', '1', '0', '0'],\n        ['1', '0', '1', '1', '1'],\n        ['1', '1', '1', '1', '1'],\n        ['1', '0', '0', '1', '0']\n    ]\n    <\/pre>\n<h2>Output Format<\/h2>\n<p>\n        Returns the area of the largest square as an integer.\n    <\/p>\n<h2>Example Input and Output<\/h2>\n<pre>\n    Input:\n    [\n        ['1', '0', '1', '0', '0'],\n        ['1', '0', '1', '1', '1'],\n        ['1', '1', '1', '1', '1'],\n        ['1', '0', '0', '1', '0']\n    ]\n    \n    Output: 4\n    <\/pre>\n<h2>Problem Solving Process<\/h2>\n<h3>1. Understanding and Analyzing the Problem<\/h3>\n<p>\n        This problem requires finding the maximum square made of &#8216;1&#8217;s in a binary matrix.<br \/>\n        To do this, we need to keep track of the possible square sizes for each element.<br \/>\n        The value of each element in the matrix is determined based on the following rules, with respect to the bottom right corner of the square.\n    <\/p>\n<p>\n        If the element at position (i, j) is &#8216;1&#8217;, then the size of the square with this position as the bottom right corner will be<br \/>\n        the minimum value among the left (i, j-1), above (i-1, j), and upper left diagonal (i-1, j-1) positions plus 1.<br \/>\n        This allows us to analyze the &#8216;1&#8217;s included in the current position and find the area of the largest square.\n    <\/p>\n<h3>2. Algorithm Design<\/h3>\n<p>\n        To solve the problem, we apply Dynamic Programming (DP) in the following steps.\n    <\/p>\n<ol>\n<li>Check the size of the given binary matrix and create a DP array.<\/li>\n<li>Iterate through each element of the binary matrix and update the DP array whenever a 1 is encountered.<\/li>\n<li>Find the maximum value in the DP array and return its square to calculate the area of the largest square.<\/li>\n<\/ol>\n<h3>3. Code Implementation<\/h3>\n<p>\n        Based on the algorithm design above, let&#8217;s implement the code in C++.<br \/>\n        Below is the final implementation code.\n    <\/p>\n<pre><code>\n#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nint maximalSquare(vector<vector<char>&gt;&amp; matrix) {\n    if (matrix.empty()) return 0;\n    \n    int maxSide = 0;\n    int rows = matrix.size(), cols = matrix[0].size();\n    vector<vector<int>&gt; dp(rows + 1, vector<int>(cols + 1, 0));\n    \n    for (int i = 1; i &lt;= rows; ++i) {\n        for (int j = 1; j &lt;= cols; ++j) {\n            if (matrix[i - 1][j - 1] == '1') {\n                dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1;\n                maxSide = max(maxSide, dp[i][j]);\n            }\n        }\n    }\n    \n    return maxSide * maxSide; \/\/ return area\n}\n\nint main() {\n    vector<vector<char>&gt; matrix = {\n        {'1', '0', '1', '0', '0'},\n        {'1', '0', '1', '1', '1'},\n        {'1', '1', '1', '1', '1'},\n        {'1', '0', '0', '1', '0'}\n    };\n    \n    cout &lt;&lt; \"The area of the largest square is: \" &lt;&lt; maximalSquare(matrix) &lt;&lt; endl;\n    return 0;\n}\n<\/vector<char><\/int><\/vector<int><\/vector<char><\/algorithm><\/vector><\/iostream><\/code><\/pre>\n<h3>4. Code Analysis<\/h3>\n<p>\n        This code creates a DP array initialized to 0 and iterates through the input matrix, updating the maximum square size at each position.<br \/>\n        The reason for using an array size of (rows + 1) x (cols + 1) is to easily handle boundary conditions in the DP array.<br \/>\n        The variable maxSide, which stores the length of the largest side, is used to ultimately calculate the area of the square.\n    <\/p>\n<h3>5. Performance Analysis<\/h3>\n<p>\n        The time complexity of this algorithm is O(n * m), and the space complexity is also O(n * m).<br \/>\n        The performance increases proportionally to the size of the matrix n x m, making it efficient for processing large matrices.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        In this article, we discussed how to solve the algorithm problem of finding the area of the largest square in a binary matrix using C++.<br \/>\n        The Dynamic Programming approach is very effective in decomposing the problem and finding the optimal solution.<br \/>\n        I hope this course provided an opportunity to deepen your understanding of data structures and algorithms in C++ through practical exercises.\n    <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description This is the problem of finding the largest square composed of 1&#8217;s in a given binary matrix. Each element of the matrix is either 0 or 1, and 1 can be included as part of the square. Determining the size of the square is based on the edge values of each 1, and &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34126\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding the Largest Square&#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-34126","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 Largest Square - \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\/34126\/\" \/>\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 Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description This is the problem of finding the largest square composed of 1&#8217;s in a given binary matrix. Each element of the matrix is either 0 or 1, and 1 can be included as part of the square. Determining the size of the square is based on the edge values of each 1, and &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding the Largest Square&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34126\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:30+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\/34126\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34126\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding the Largest Square\",\"datePublished\":\"2024-11-01T09:24:29+00:00\",\"dateModified\":\"2024-11-01T10:58:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34126\/\"},\"wordCount\":488,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34126\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34126\/\",\"name\":\"C++ Coding Test Course, Finding the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:29+00:00\",\"dateModified\":\"2024-11-01T10:58:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34126\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34126\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34126\/#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 Largest Square\"}]},{\"@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 Largest Square - \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\/34126\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description This is the problem of finding the largest square composed of 1&#8217;s in a given binary matrix. Each element of the matrix is either 0 or 1, and 1 can be included as part of the square. Determining the size of the square is based on the edge values of each 1, and &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding the Largest Square\"","og_url":"https:\/\/atmokpo.com\/w\/34126\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:29+00:00","article_modified_time":"2024-11-01T10:58:30+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\/34126\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34126\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding the Largest Square","datePublished":"2024-11-01T09:24:29+00:00","dateModified":"2024-11-01T10:58:30+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34126\/"},"wordCount":488,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34126\/","url":"https:\/\/atmokpo.com\/w\/34126\/","name":"C++ Coding Test Course, Finding the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:29+00:00","dateModified":"2024-11-01T10:58:30+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34126\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34126\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34126\/#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 Largest Square"}]},{"@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\/34126","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=34126"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34126\/revisions"}],"predecessor-version":[{"id":34127,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34126\/revisions\/34127"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}