{"id":33578,"date":"2024-11-01T09:18:05","date_gmt":"2024-11-01T09:18:05","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33578"},"modified":"2024-11-01T11:47:33","modified_gmt":"2024-11-01T11:47:33","slug":"python-coding-test-course-finding-the-largest-square","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33578\/","title":{"rendered":"Python Coding Test Course, Finding the Largest Square"},"content":{"rendered":"<p><body><\/p>\n<p>One of the common problems that often appears in coding tests is finding the largest square in a two-dimensional array. This problem can be defined as follows.<\/p>\n<h2>Problem Definition<\/h2>\n<p>Given a binary matrix (a two-dimensional array consisting of 0s and 1s), find the area of the largest square composed of 1s.<\/p>\n<p>For example, let&#8217;s assume we have the following matrix.<\/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<p>In the matrix above, the largest square is the area from (1, 1) to (3, 3), with an area of 9.<\/p>\n<h2>Problem Approach and Solution Process<\/h2>\n<p>To solve this problem, we can use the Dynamic Programming method. This methodology breaks the problem down into smaller subproblems and uses the results to derive the final outcome.<\/p>\n<h3>Approach Using Dynamic Programming<\/h3>\n<p>1. First, we will get the size of the given matrix and define a 2D array dp for dynamic programming. Each element of this dp array will represent the length of one side of the largest square at a specific position.<\/p>\n<p>2. Initialization: Initialize each element of the dp array to 0. However, the first row and first column of the given matrix should be set to 1 only for positions in the original matrix where there is a 1.<\/p>\n<p>3. Filling the dp array: By examining each element of the binary matrix, if the value at a specific position (i, j) is 1, then dp[i][j] is set to the minimum of dp[i-1][j], dp[i][j-1], dp[i-1][j-1] plus 1. This is how we calculate the sides of the largest square.<\/p>\n<p>4. Result: We need to find the largest value in the dp array, and the square of this value will be the area of the square we are looking for.<\/p>\n<h3>Implementation of Dynamic Programming<\/h3>\n<p>Now, let&#8217;s write a Python code based on the above method.<\/p>\n<pre>\n    <code>\ndef maximalSquare(matrix):\n    if not matrix:\n        return 0\n\n    max_square_len = 0\n    rows = len(matrix)\n    cols = len(matrix[0])\n    dp = [[0] * cols for _ in range(rows)]\n\n    for i in range(rows):\n        for j in range(cols):\n            if matrix[i][j] == '1':\n                if i == 0 or j == 0:\n                    dp[i][j] = 1\n                else:\n                    dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1\n\n                max_square_len = max(max_square_len, dp[i][j])\n\n    return max_square_len * max_square_len\n    <\/code>\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<p>The first line of the code checks whether the given matrix is empty. If it is empty, the result is 0.<\/p>\n<p>Next, it retrieves the number of rows and columns in the matrix and creates the dp array. This array has the same size as the given binary matrix and is initialized to 0.<\/p>\n<p>Using a loop, we iterate through each element, processing only when the current element is 1. During this process, we fill the dp array using the previously mentioned relationships. Here, we always track and update the length of the largest square&#8217;s side.<\/p>\n<p>Finally, we return the area of the largest square.<\/p>\n<h2>Complexity Analysis<\/h2>\n<p>The above algorithm has a time complexity of O(n * m), where n is the number of rows in the matrix and m is the number of columns. The space complexity is O(n * m), which is the space needed to store the dp array.<\/p>\n<h2>Let&#8217;s Try with an Example<\/h2>\n<p>Now, let&#8217;s use this function to find the largest square in the given matrix.<\/p>\n<pre>\n    <code>\nmatrix = [\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]\nprint(maximalSquare(matrix))  # 9\n    <\/code>\n    <\/pre>\n<h2>Conclusion<\/h2>\n<p>This problem is one of the frequently encountered types in coding tests. If you can understand and solve this problem well through practice, it will greatly help you to solve similar problems. Moreover, the ability to understand and apply the principles of dynamic programming is useful for solving various algorithmic problems.<\/p>\n<p>Additionally, you can also tackle more complex variations of this problem by extending the binary matrix into more intricate forms. For example, there are various types of problems where the structure of 0s and 1s changes in the given matrix, or where you need to find different shapes of squares or rectangles that satisfy given conditions.<\/p>\n<h2>Moving Forward<\/h2>\n<p>Continue to practice solving more algorithm problems and optimizing by considering time complexity, space complexity, and so on. Deepening your understanding of frequently used data structures and algorithms is also very important. Furthermore, as you encounter various problems, your problem-solving skills will naturally improve.<\/p>\n<p>We hope to help you grow your coding skills through many algorithm courses in the future!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the common problems that often appears in coding tests is finding the largest square in a two-dimensional array. This problem can be defined as follows. Problem Definition Given a binary matrix (a two-dimensional array consisting of 0s and 1s), find the area of the largest square composed of 1s. For example, let&#8217;s assume &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33578\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python 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":[145],"tags":[],"class_list":["post-33578","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python 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\/33578\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Finding the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"One of the common problems that often appears in coding tests is finding the largest square in a two-dimensional array. This problem can be defined as follows. Problem Definition Given a binary matrix (a two-dimensional array consisting of 0s and 1s), find the area of the largest square composed of 1s. For example, let&#8217;s assume &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Finding the Largest Square&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33578\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:33+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\/33578\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33578\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Finding the Largest Square\",\"datePublished\":\"2024-11-01T09:18:05+00:00\",\"dateModified\":\"2024-11-01T11:47:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33578\/\"},\"wordCount\":639,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33578\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33578\/\",\"name\":\"Python Coding Test Course, Finding the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:05+00:00\",\"dateModified\":\"2024-11-01T11:47:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33578\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33578\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33578\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python 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":"Python 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\/33578\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Finding the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"One of the common problems that often appears in coding tests is finding the largest square in a two-dimensional array. This problem can be defined as follows. Problem Definition Given a binary matrix (a two-dimensional array consisting of 0s and 1s), find the area of the largest square composed of 1s. For example, let&#8217;s assume &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Finding the Largest Square\"","og_url":"https:\/\/atmokpo.com\/w\/33578\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:05+00:00","article_modified_time":"2024-11-01T11:47:33+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\/33578\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33578\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Finding the Largest Square","datePublished":"2024-11-01T09:18:05+00:00","dateModified":"2024-11-01T11:47:33+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33578\/"},"wordCount":639,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33578\/","url":"https:\/\/atmokpo.com\/w\/33578\/","name":"Python Coding Test Course, Finding the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:05+00:00","dateModified":"2024-11-01T11:47:33+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33578\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33578\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33578\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python 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\/33578","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=33578"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33578\/revisions"}],"predecessor-version":[{"id":33579,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33578\/revisions\/33579"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}