{"id":34970,"date":"2024-11-01T09:34:09","date_gmt":"2024-11-01T09:34:09","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34970"},"modified":"2024-11-01T12:53:28","modified_gmt":"2024-11-01T12:53:28","slug":"%ec%bd%94%ed%8b%80%eb%a6%b0-%ec%bd%94%eb%94%a9%ed%85%8c%ec%8a%a4%ed%8a%b8-%ea%b0%95%ec%a2%8c-%ea%b9%8a%ec%9d%b4-%ec%9a%b0%ec%84%a0-%ed%83%90%ec%83%89-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34970\/","title":{"rendered":"Kotlin coding test course, depth-first search"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>Introduction<\/h2>\n<p>\n                Hello! In this post, we will explore the Depth-First Search (DFS) algorithm. DFS is a search algorithm used in graph or tree structures that explores as deeply as possible before backtracking to adjacent nodes. It is a crucial topic often featured in coding tests; therefore, I recommend mastering it to enhance your understanding of algorithms and your problem-solving skills.\n            <\/p>\n<\/section>\n<section>\n<h2>Problem Description<\/h2>\n<h3>Problem Title: Number of Islands<\/h3>\n<p>\n                In the given 2D array, &#8216;1&#8217; represents land and &#8216;0&#8217; represents water. Continuous &#8216;1&#8217;s are considered a single island. All &#8216;1&#8217;s that are connected horizontally or vertically to land &#8216;1&#8217; are part of the same island. Calculate the number of islands in the given array.\n            <\/p>\n<h3>Input Example<\/h3>\n<pre><code>[\n    [1, 1, 0, 0, 0],\n    [0, 1, 0, 0, 1],\n    [1, 0, 0, 1, 1],\n    [0, 0, 0, 0, 0],\n    [1, 0, 1, 0, 1]\n]<\/code><\/pre>\n<h3>Output Example<\/h3>\n<p>Number of islands: 5<\/p>\n<\/section>\n<section>\n<h2>Solution Process<\/h2>\n<p>This problem can be solved using the DFS algorithm. Let&#8217;s go through the steps to solve the problem.<\/p>\n<h3>Step 1: Graph Representation<\/h3>\n<p>\n                The problem deals with a graph represented as a 2D array. Each &#8216;1&#8217; represents land, and to solve this problem, we need to determine how these lands are connected.\n            <\/p>\n<h3>Step 2: Implementing the DFS Algorithm<\/h3>\n<p>\n                When visiting each land &#8216;1&#8217; using DFS, we need to group them as a single island. To do this, we define a recursive function to visit adjacent land &#8216;1&#8217;s.\n            <\/p>\n<h3>Step 3: Writing the Code<\/h3>\n<p>\n                Now, let&#8217;s write code to implement the basic DFS algorithm. We can implement it using Kotlin as follows.\n            <\/p>\n<pre><code>fun numIslands(grid: Array<chararray>): Int {\n    if (grid.isEmpty()) return 0\n    var count = 0\n\n    fun dfs(i: Int, j: Int) {\n        if (i &lt; 0 || i &gt;= grid.size || j &lt; 0 || j &gt;= grid[0].size || grid[i][j] == '0') {\n            return\n        }\n        grid[i][j] = '0' \/\/ Mark as visited\n        dfs(i - 1, j) \/\/ up\n        dfs(i + 1, j) \/\/ down\n        dfs(i, j - 1) \/\/ left\n        dfs(i, j + 1) \/\/ right\n    }\n\n    for (i in grid.indices) {\n        for (j in grid[i].indices) {\n            if (grid[i][j] == '1') {\n                count++\n                dfs(i, j) \/\/ new island found\n            }\n        }\n    }\n    return count\n}<\/chararray><\/code><\/pre>\n<h3>Step 4: Code Explanation<\/h3>\n<p>\n                In the above code, the <code>numIslands<\/code> function takes a 2D array as a parameter and returns the number of islands.<br \/>\n                The internal <code>dfs<\/code> function recursively visits each land, changing already visited lands to &#8216;0&#8217; to prevent duplicate visits.<br \/>\n                The double loop checks each element of the array, increasing the island count whenever a &#8216;1&#8217; is found and calling <code>dfs<\/code>.\n            <\/p>\n<h3>Step 5: Time Complexity and Space Complexity<\/h3>\n<p>\n                The time complexity of the DFS algorithm is O(M * N), where M is the number of rows and N is the number of columns.<br \/>\n                The space complexity is O(H), where H is the depth of the recursive call stack. In the worst case, it can be O(M + N).\n            <\/p>\n<h2>Test Case Analysis<\/h2>\n<p>\n                We have written a basic code based on the above description, but it is important to verify the accuracy of the code through various test cases.<br \/>\n                Here are descriptions of some test cases.\n            <\/p>\n<ol>\n<li>\n<strong>Test Case 1:<\/strong><br \/>\n                    Input: <\/p>\n<pre><code>[[1,1,0,0],[0,1,0,0],[0,0,0,1],[1,0,0,1]]<\/code><\/pre>\n<p>                    Expected Output: 2\n                <\/li>\n<li>\n<strong>Test Case 2:<\/strong><br \/>\n                    Input: <\/p>\n<pre><code>[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]<\/code><\/pre>\n<p>                    Expected Output: 0\n                <\/li>\n<li>\n<strong>Test Case 3:<\/strong><br \/>\n                    Input: <\/p>\n<pre><code>[[1,1,1,1],[1,0,0,0],[1,1,1,1]]<\/code><\/pre>\n<p>                    Expected Output: 1\n                <\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n                In this post, we explained Depth-First Search (DFS) and implemented the algorithm through a sample problem.<br \/>\n                DFS is a very useful tool when solving graph problems, so you will often encounter it in coding tests.<br \/>\n                Practice solving various problems to understand and utilize DFS in depth.\n            <\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Hello! In this post, we will explore the Depth-First Search (DFS) algorithm. DFS is a search algorithm used in graph or tree structures that explores as deeply as possible before backtracking to adjacent nodes. It is a crucial topic often featured in coding tests; therefore, I recommend mastering it to enhance your understanding of &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34970\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Kotlin coding test course, depth-first search&#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-34970","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, depth-first search - \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\/34970\/\" \/>\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, depth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Introduction Hello! In this post, we will explore the Depth-First Search (DFS) algorithm. DFS is a search algorithm used in graph or tree structures that explores as deeply as possible before backtracking to adjacent nodes. It is a crucial topic often featured in coding tests; therefore, I recommend mastering it to enhance your understanding of &hellip; \ub354 \ubcf4\uae30 &quot;Kotlin coding test course, depth-first search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34970\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:34:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:53:28+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\/34970\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34970\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Kotlin coding test course, depth-first search\",\"datePublished\":\"2024-11-01T09:34:09+00:00\",\"dateModified\":\"2024-11-01T12:53:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34970\/\"},\"wordCount\":442,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34970\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34970\/\",\"name\":\"Kotlin coding test course, depth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:34:09+00:00\",\"dateModified\":\"2024-11-01T12:53:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34970\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34970\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34970\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Kotlin coding test course, depth-first search\"}]},{\"@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, depth-first search - \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\/34970\/","og_locale":"ko_KR","og_type":"article","og_title":"Kotlin coding test course, depth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Introduction Hello! In this post, we will explore the Depth-First Search (DFS) algorithm. DFS is a search algorithm used in graph or tree structures that explores as deeply as possible before backtracking to adjacent nodes. It is a crucial topic often featured in coding tests; therefore, I recommend mastering it to enhance your understanding of &hellip; \ub354 \ubcf4\uae30 \"Kotlin coding test course, depth-first search\"","og_url":"https:\/\/atmokpo.com\/w\/34970\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:34:09+00:00","article_modified_time":"2024-11-01T12:53:28+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\/34970\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34970\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Kotlin coding test course, depth-first search","datePublished":"2024-11-01T09:34:09+00:00","dateModified":"2024-11-01T12:53:28+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34970\/"},"wordCount":442,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34970\/","url":"https:\/\/atmokpo.com\/w\/34970\/","name":"Kotlin coding test course, depth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:34:09+00:00","dateModified":"2024-11-01T12:53:28+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34970\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34970\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34970\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Kotlin coding test course, depth-first search"}]},{"@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\/34970","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=34970"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34970\/revisions"}],"predecessor-version":[{"id":38113,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34970\/revisions\/38113"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}