{"id":34938,"date":"2024-11-01T09:33:48","date_gmt":"2024-11-01T09:33:48","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34938"},"modified":"2024-11-01T11:45:40","modified_gmt":"2024-11-01T11:45:40","slug":"kotlin-coding-test-course-finding-the-longest-increasing-subsequence","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34938\/","title":{"rendered":"kotlin coding test course, finding the longest increasing subsequence"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this post, we will address the problem of &#8220;Longest Increasing Subsequence&#8221; that is frequently asked in coding tests. This problem is very helpful for understanding the basics of algorithms and building your skills. I will explain the theories for effectively solving the problem and the specific implementation process using Kotlin.<\/p>\n<h2>Problem Description<\/h2>\n<p>The task is to find the Longest Increasing Subsequence (LIS) in a given array. A subsequence is an array formed by selecting elements from the original array in order, while maintaining the order of the original array. An increasing sequence means the elements are sorted in ascending order.<\/p>\n<h3>Example<\/h3>\n<pre>\n    Input: [10, 22, 9, 33, 21, 50, 41, 60, 80]\n    Output: 6\n    Explanation: An example of the longest increasing subsequence is [10, 22, 33, 50, 60, 80].\n    <\/pre>\n<h2>Approach to Solution<\/h2>\n<p>The basic approach to solving this problem is to use dynamic programming (DP). DP is a method for breaking down a problem into smaller subproblems and storing previous computation results to avoid repeating the same calculations.<\/p>\n<h3>Step 1: Initialize the DP Table<\/h3>\n<p>First, we initialize the DP table. We create an array to store the LIS length starting at each index of the input array. Initially, since each element itself forms a subsequence, all values are set to 1.<\/p>\n<h3>Step 2: Update the DP Table<\/h3>\n<p>Now, we compare all elements of the input array with each other. For each element <code>arr[i]<\/code>, we check all previous elements <code>arr[j]<\/code> (j &lt; i) and evaluate the following condition:<\/p>\n<pre>\n    if arr[i] &gt; arr[j] then\n        dp[i] = max(dp[i], dp[j] + 1)\n    <\/pre>\n<p>Here, <code>dp[i]<\/code> is the length of the LIS that includes <code>arr[i]<\/code>, and <code>dp[j] + 1<\/code> represents the length if <code>arr[j]<\/code> is followed by <code>arr[i]<\/code>.<\/p>\n<h3>Step 3: Derive the Result<\/h3>\n<p>After processing all elements, we find the largest value in the <code>dp<\/code> array and return the length of the LIS as the result.<\/p>\n<h2>Kotlin Implementation<\/h2>\n<p>Now, let\u2019s implement it in Kotlin. Below is the code that follows the algorithm explained above.<\/p>\n<pre><code>\nfun longestIncreasingSubsequence(arr: IntArray): Int {\n    val n = arr.size\n    val dp = IntArray(n) { 1 } \/\/ initialize dp array\n\n    for (i in 1 until n) {\n        for (j in 0 until i) {\n            if (arr[i] &gt; arr[j]) {\n                dp[i] = maxOf(dp[i], dp[j] + 1)\n            }\n        }\n    }\n    return dp.maxOrNull() ?: 1 \/\/ return the maximum value in dp array\n}\n\nfun main() {\n    val arr = intArrayOf(10, 22, 9, 33, 21, 50, 41, 60, 80)\n    val length = longestIncreasingSubsequence(arr)\n    println(\"Length of the longest increasing subsequence: $length\")\n}\n    <\/code><\/pre>\n<h2>Complexity Analysis<\/h2>\n<p>As a result, the time complexity of this algorithm is O(n\u00b2). This is because a nested loop runs for n. The space complexity is O(n) as we need space to store the DP array.<\/p>\n<h2>Optimization Method<\/h2>\n<p>If higher performance is required, the LIS problem can be solved using binary search, achieving O(n log n). This method is implemented as follows:<\/p>\n<pre><code>\nimport java.util.*\n\nfun longestIncreasingSubsequenceOptimized(arr: IntArray): Int {\n    if (arr.isEmpty()) return 0\n\n    val tails = mutableListOf<Int>()\n\n    for (num in arr) {\n        val index = tails.binarySearch(num)\n        if (index &lt; 0) {\n            val pos = -(index + 1)\n            if (pos == tails.size) {\n                tails.add(num)\n            } else {\n                tails[pos] = num\n            }\n        }\n    }\n    return tails.size\n}\n    <\/code><\/pre>\n<p>Here, the <code>tails<\/code> array stores the possible last elements of the LIS. Using binary search, it finds the appropriate position to insert or replace elements.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we explored the longest increasing subsequence problem using Kotlin. I detailed the approach to the problem and the implementation method, along with optimization techniques. Such problems are frequently asked in coding tests, so repeated practice is important. I will continue to share various algorithm problems. If you have any questions, please ask in the comments!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this post, we will address the problem of &#8220;Longest Increasing Subsequence&#8221; that is frequently asked in coding tests. This problem is very helpful for understanding the basics of algorithms and building your skills. I will explain the theories for effectively solving the problem and the specific implementation process using Kotlin. Problem Description The &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34938\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, finding the longest increasing subsequence&#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-34938","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 longest increasing subsequence - \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\/34938\/\" \/>\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 longest increasing subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this post, we will address the problem of &#8220;Longest Increasing Subsequence&#8221; that is frequently asked in coding tests. This problem is very helpful for understanding the basics of algorithms and building your skills. I will explain the theories for effectively solving the problem and the specific implementation process using Kotlin. Problem Description The &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, finding the longest increasing subsequence&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34938\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:33:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:45:40+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\/34938\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34938\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, finding the longest increasing subsequence\",\"datePublished\":\"2024-11-01T09:33:48+00:00\",\"dateModified\":\"2024-11-01T11:45:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34938\/\"},\"wordCount\":435,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34938\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34938\/\",\"name\":\"kotlin coding test course, finding the longest increasing subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:33:48+00:00\",\"dateModified\":\"2024-11-01T11:45:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34938\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34938\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34938\/#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 longest increasing subsequence\"}]},{\"@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 longest increasing subsequence - \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\/34938\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, finding the longest increasing subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this post, we will address the problem of &#8220;Longest Increasing Subsequence&#8221; that is frequently asked in coding tests. This problem is very helpful for understanding the basics of algorithms and building your skills. I will explain the theories for effectively solving the problem and the specific implementation process using Kotlin. Problem Description The &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, finding the longest increasing subsequence\"","og_url":"https:\/\/atmokpo.com\/w\/34938\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:33:48+00:00","article_modified_time":"2024-11-01T11:45:40+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\/34938\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34938\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, finding the longest increasing subsequence","datePublished":"2024-11-01T09:33:48+00:00","dateModified":"2024-11-01T11:45:40+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34938\/"},"wordCount":435,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34938\/","url":"https:\/\/atmokpo.com\/w\/34938\/","name":"kotlin coding test course, finding the longest increasing subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:33:48+00:00","dateModified":"2024-11-01T11:45:40+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34938\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34938\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34938\/#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 longest increasing subsequence"}]},{"@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\/34938","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=34938"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34938\/revisions"}],"predecessor-version":[{"id":34939,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34938\/revisions\/34939"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}