{"id":34672,"date":"2024-11-01T09:30:43","date_gmt":"2024-11-01T09:30:43","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34672"},"modified":"2024-11-01T11:27:03","modified_gmt":"2024-11-01T11:27:03","slug":"swift-coding-test-course-find-the-largest-square","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34672\/","title":{"rendered":"Swift Coding Test Course, Find the Largest Square"},"content":{"rendered":"<p><body><\/p>\n<p>Algorithm problems play an important role in developing problem-solving skills for programmers. In this article, we will address the problem of finding the largest square using Swift. The goal is to find the area of the largest square in a given 2D array.<\/p>\n<h2>Problem Description<\/h2>\n<p>Given a binary matrix, write a function to find the largest square containing only 1s and return its area. 0 represents an empty cell, and 1 represents a filled cell.<\/p>\n<h3>Example<\/h3>\n<pre>\nInput:\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\nOutput: 4\n<\/pre>\n<h2>Problem Solving Process<\/h2>\n<h3>Approach to the Problem<\/h3>\n<p>To solve this problem, we will use a dynamic programming approach. Below are the steps to tackle the problem.<\/p>\n<ol>\n<li>Create a DP table based on the given binary matrix.<\/li>\n<li>Calculate the size of the largest square at each cell.<\/li>\n<li>More specifically, while traversing each element of the matrix, if that element is 1, update it to be the minimum value of the element to the right, below, and diagonally below plus one.<\/li>\n<li>Calculate and return the area of the largest square found.<\/li>\n<\/ol>\n<h3>Dynamic Programming Table Construction<\/h3>\n<p>The size of the DP table will be the same as the input matrix, and the values at each element will be set according to the following rules.<\/p>\n<ul>\n<li>If it is 0: Set to 0 since no square can be formed at that position.<\/li>\n<li>If it is 1: Calculate the size of the square by referencing the adjacent values as described above.<\/li>\n<\/ul>\n<h3>Swift Code Implementation<\/h3>\n<p>Now let&#8217;s implement the approach described above in Swift code.<\/p>\n<pre><code>\nfunc maximalSquare(_ matrix: [[Character]]) -> Int {\n    guard !matrix.isEmpty else { return 0 }\n    let rows = matrix.count\n    let cols = matrix[0].count\n    var dp = Array(repeating: Array(repeating: 0, count: cols), count: rows)\n    var maxSide = 0\n\n    for i in 0..<rows {\n        for j in 0..<cols {\n            if i == 0 || j == 0 {\n                dp[i][j] = matrix[i][j] == \"1\" ? 1 : 0\n            } else if matrix[i][j] == \"1\" {\n                dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1\n            }\n            maxSide = max(maxSide, dp[i][j])\n        }\n    }\n    return maxSide * maxSide\n}\n<\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<p>Let me explain all the variables used in the code above:<\/p>\n<ul>\n<li><strong>rows<\/strong>: The number of rows in the matrix<\/li>\n<li><strong>cols<\/strong>: The number of columns in the matrix<\/li>\n<li><strong>dp<\/strong>: The dynamic programming table storing the size of each square<\/li>\n<li><strong>maxSide<\/strong>: The length of the side of the largest square found so far<\/li>\n<\/ul>\n<h3>Test Cases<\/h3>\n<p>Now let's test the code we wrote. Below are some test cases.<\/p>\n<pre><code>\nlet testMatrix1: [[Character]] = [\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\nlet result1 = maximalSquare(testMatrix1)\nprint(result1) \/\/ 4\n\nlet testMatrix2: [[Character]] = [\n    [\"0\", \"1\"],\n    [\"1\", \"0\"]\n]\n\nlet result2 = maximalSquare(testMatrix2)\nprint(result2) \/\/ 1\n<\/code><\/pre>\n<h3>Conclusion<\/h3>\n<p>In this tutorial, we learned how to solve the problem of finding the largest square using Swift. This problem can be efficiently solved through the application of dynamic programming and is commonly encountered in coding interviews. Solving real algorithm problems is very beneficial, and I hope that through practice, you can elevate your problem-solving skills to the next level.<\/p>\n<h2>References<\/h2>\n<p>If you want to learn more about this problem in depth, please refer to the following resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/largest-square-formed-in-a-matrix\/\" target=\"_blank\" rel=\"noopener\">Largest Square Formed in a Matrix - GeeksforGeeks<\/a><\/li>\n<li><a href=\"https:\/\/leetcode.com\/problems\/maximal-square\/\" target=\"_blank\" rel=\"noopener\">Maximal Square - LeetCode<\/a><\/li>\n<li><a href=\"https:\/\/medium.com\/swlh\/understanding-dynamic-programming-with-maximum-square-14eb7d9f6548\" target=\"_blank\" rel=\"noopener\">Understanding Dynamic Programming with Maximum Square - Medium<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Algorithm problems play an important role in developing problem-solving skills for programmers. In this article, we will address the problem of finding the largest square using Swift. The goal is to find the area of the largest square in a given 2D array. Problem Description Given a binary matrix, write a function to find the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34672\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Swift Coding Test Course, Find 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":[129],"tags":[],"class_list":["post-34672","post","type-post","status-publish","format-standard","hentry","category-swift-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift Coding Test Course, Find 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\/34672\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Coding Test Course, Find the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Algorithm problems play an important role in developing problem-solving skills for programmers. In this article, we will address the problem of finding the largest square using Swift. The goal is to find the area of the largest square in a given 2D array. Problem Description Given a binary matrix, write a function to find the &hellip; \ub354 \ubcf4\uae30 &quot;Swift Coding Test Course, Find the Largest Square&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34672\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:30:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:27:03+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=\"1\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34672\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34672\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Swift Coding Test Course, Find the Largest Square\",\"datePublished\":\"2024-11-01T09:30:43+00:00\",\"dateModified\":\"2024-11-01T11:27:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34672\/\"},\"wordCount\":413,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34672\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34672\/\",\"name\":\"Swift Coding Test Course, Find the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:30:43+00:00\",\"dateModified\":\"2024-11-01T11:27:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34672\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34672\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34672\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift Coding Test Course, Find 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":"Swift Coding Test Course, Find 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\/34672\/","og_locale":"ko_KR","og_type":"article","og_title":"Swift Coding Test Course, Find the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Algorithm problems play an important role in developing problem-solving skills for programmers. In this article, we will address the problem of finding the largest square using Swift. The goal is to find the area of the largest square in a given 2D array. Problem Description Given a binary matrix, write a function to find the &hellip; \ub354 \ubcf4\uae30 \"Swift Coding Test Course, Find the Largest Square\"","og_url":"https:\/\/atmokpo.com\/w\/34672\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:30:43+00:00","article_modified_time":"2024-11-01T11:27:03+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":"1\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34672\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34672\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Swift Coding Test Course, Find the Largest Square","datePublished":"2024-11-01T09:30:43+00:00","dateModified":"2024-11-01T11:27:03+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34672\/"},"wordCount":413,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34672\/","url":"https:\/\/atmokpo.com\/w\/34672\/","name":"Swift Coding Test Course, Find the Largest Square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:30:43+00:00","dateModified":"2024-11-01T11:27:03+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34672\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34672\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34672\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Swift Coding Test Course, Find 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\/34672","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=34672"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34672\/revisions"}],"predecessor-version":[{"id":34673,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34672\/revisions\/34673"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}