{"id":34942,"date":"2024-11-01T09:33:51","date_gmt":"2024-11-01T09:33:51","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34942"},"modified":"2024-11-01T12:56:03","modified_gmt":"2024-11-01T12:56:03","slug":"%d0%ba%d0%be%d1%82%d0%bb%d0%b8%d0%bd-%d0%ba%d0%be%d0%b4%d0%b8%d0%bd%d0%b3-%d1%82%d0%b5%d1%81%d1%82-%d0%ba%d1%83%d1%80%d1%81-%d0%bf%d0%be%d0%b8%d1%81%d0%ba-%d0%bd%d0%b0%d0%b8%d0%b1%d0%be%d0%bb%d1%8c","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34942\/","title":{"rendered":"Kotlin coding test course, finding the largest square"},"content":{"rendered":"<div class=\"blog-post\">\n<p>Hello, and welcome to the coding test problem-solving course using Kotlin. In this session, we will solve the problem called &#8220;Finding the Largest Square.&#8221; The task is to find the area of the largest square in a given 2D array. Through this problem, you will learn the basic concept of Dynamic Programming and understand the syntax and features of the Kotlin language.<\/p>\n<h2>Problem Description<\/h2>\n<p>There is a given 2D binary array of size m x n. In this array, &#8216;1&#8217; indicates that a square can be formed at that position, while &#8216;0&#8217; indicates it cannot. The goal is to find the area of the largest square.<\/p>\n<p>For example, consider the following array:<\/p>\n<pre>\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    <\/pre>\n<p>In this case, the area of the largest square is 4. (2&#215;2 square)<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>To solve this problem, we will use Dynamic Programming. Here are the main steps to solve the problem:<\/p>\n<ol>\n<li><strong>Initialize DP Array:<\/strong> Create a DP array with the same size as the input 2D array. Each element in this DP array represents the length of the side of the square ending at position (i, j).<\/li>\n<li><strong>Set State Transition Relation:<\/strong> If grid[i][j] is &#8216;1&#8217;, then DP[i][j] will be the minimum of DP[i-1][j], DP[i][j-1], and DP[i-1][j-1] + 1. This determines the length of the largest square at (i,j).<\/li>\n<li><strong>Track Maximum Square Side Length:<\/strong> Through this process, track the side length of the largest square and finally calculate the area by squaring that value.<\/li>\n<\/ol>\n<h2>Code Implementation<\/h2>\n<p>Now let&#8217;s implement the above algorithm in Kotlin. Below is the code for the algorithm:<\/p>\n<pre><code>\n    fun maximalSquare(matrix: Array<chararray>): Int {\n        if (matrix.isEmpty()) return 0\n\n        val m = matrix.size\n        val n = matrix[0].size\n        val dp = Array(m) { IntArray(n) }\n        var maxSide = 0\n\n        for (i in 0 until m) {\n            for (j in 0 until n) {\n                if (matrix[i][j] == '1') {\n                    if (i == 0 || j == 0) {\n                        dp[i][j] = 1 \/\/ For the first row or first column\n                    } else {\n                        dp[i][j] = minOf(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1\n                    }\n                    maxSide = maxOf(maxSide, dp[i][j])\n                }\n            }\n        }\n        return maxSide * maxSide \/\/ Return the area of the square\n    }\n    <\/chararray><\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>The code takes a 2D array as input and calculates the length of the side of the square ending at each cell. Here is an explanation of the key parts of the code:<\/p>\n<ul>\n<li><strong>Initial Validation:<\/strong> Checks whether the input matrix is empty. If it is, returns 0.<\/li>\n<li><strong>Double Loop:<\/strong> Iterates through each element, updating the values in the DP table if it is &#8216;1&#8217;.<\/li>\n<li><strong>Maximum Side Update:<\/strong> Tracks the maximum value at each case and returns the final result by squaring it.<\/li>\n<\/ul>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of this algorithm is O(m * n), and since it uses a DP array, the space complexity is also O(m * n). However, the space can be optimized. By using two variables instead of a DP array to store the information of the current row and the previous row, the space complexity can be reduced to O(n).<\/p>\n<h2>Optional Space Optimization Code<\/h2>\n<pre><code>\n    fun maximalSquare(matrix: Array<chararray>): Int {\n        if (matrix.isEmpty()) return 0\n        val m = matrix.size\n        val n = matrix[0].size\n        val dp = IntArray(n)\n        var maxSide = 0\n        var prev = 0\n\n        for (i in 0 until m) {\n            for (j in 0 until n) {\n                val temp = dp[j]\n                if (matrix[i][j] == '1') {\n                    dp[j] = if (i == 0 || j == 0) 1 else minOf(dp[j], dp[j - 1], prev) + 1\n                    maxSide = maxOf(maxSide, dp[j])\n                } else {\n                    dp[j] = 0\n                }\n                prev = temp\n            }\n        }\n        return maxSide * maxSide\n    }\n    <\/chararray><\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this course, we learned how to solve the &#8220;Finding the Largest Square&#8221; problem. In this process, we understood the basic concepts of Dynamic Programming and learned how to implement algorithms in the Kotlin language. These algorithmic problems will help improve problem-solving skills and greatly assist in preparing for coding tests. In the next session, we will cover another interesting algorithm problem. Thank you!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello, and welcome to the coding test problem-solving course using Kotlin. In this session, we will solve the problem called &#8220;Finding the Largest Square.&#8221; The task is to find the area of the largest square in a given 2D array. Through this problem, you will learn the basic concept of Dynamic Programming and understand the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34942\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Kotlin 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":[106],"tags":[],"class_list":["post-34942","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Kotlin 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\/34942\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin coding test course, finding the largest square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello, and welcome to the coding test problem-solving course using Kotlin. In this session, we will solve the problem called &#8220;Finding the Largest Square.&#8221; The task is to find the area of the largest square in a given 2D array. Through this problem, you will learn the basic concept of Dynamic Programming and understand the &hellip; \ub354 \ubcf4\uae30 &quot;Kotlin coding test course, finding the largest square&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34942\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:33:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:56: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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34942\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34942\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Kotlin coding test course, finding the largest square\",\"datePublished\":\"2024-11-01T09:33:51+00:00\",\"dateModified\":\"2024-11-01T12:56:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34942\/\"},\"wordCount\":484,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34942\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34942\/\",\"name\":\"Kotlin coding test course, finding the largest square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:33:51+00:00\",\"dateModified\":\"2024-11-01T12:56:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34942\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34942\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34942\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Kotlin 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":"Kotlin 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\/34942\/","og_locale":"ko_KR","og_type":"article","og_title":"Kotlin coding test course, finding the largest square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello, and welcome to the coding test problem-solving course using Kotlin. In this session, we will solve the problem called &#8220;Finding the Largest Square.&#8221; The task is to find the area of the largest square in a given 2D array. Through this problem, you will learn the basic concept of Dynamic Programming and understand the &hellip; \ub354 \ubcf4\uae30 \"Kotlin coding test course, finding the largest square\"","og_url":"https:\/\/atmokpo.com\/w\/34942\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:33:51+00:00","article_modified_time":"2024-11-01T12:56: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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34942\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34942\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Kotlin coding test course, finding the largest square","datePublished":"2024-11-01T09:33:51+00:00","dateModified":"2024-11-01T12:56:03+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34942\/"},"wordCount":484,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34942\/","url":"https:\/\/atmokpo.com\/w\/34942\/","name":"Kotlin coding test course, finding the largest square - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:33:51+00:00","dateModified":"2024-11-01T12:56:03+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34942\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34942\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34942\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Kotlin 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\/34942","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=34942"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34942\/revisions"}],"predecessor-version":[{"id":38120,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34942\/revisions\/38120"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}