{"id":34846,"date":"2024-11-01T09:32:40","date_gmt":"2024-11-01T09:32:40","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34846"},"modified":"2024-11-01T11:26:16","modified_gmt":"2024-11-01T11:26:16","slug":"swift-coding-test-course-queueing","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34846\/","title":{"rendered":"Swift Coding Test Course, Queueing"},"content":{"rendered":"<div class=\"blog-post\">\n<p>\n        Hello! Today, as part of the Swift coding test course, we will address the queueing problem.<br \/>\n        This is one of the frequently encountered problems in coding interviews and is an algorithm that is widely used in actual work environments.<br \/>\n        In this post, I will explain the definition of the problem, the solution method, a Swift code example for it, and optimization strategies in detail.\n    <\/p>\n<h2>Problem Definition<\/h2>\n<p>\n        There are several students standing in line. Given each student&#8217;s number,<br \/>\n        the problem is to calculate how many exchanges are needed for the students to be arranged in the correct order.<br \/>\n        Each student knows their position in line, and each student&#8217;s<br \/>\n        number is given as a natural number from 1 to N. Only two students can swap positions at a time, and<br \/>\n        the goal is to solve the problem with the minimum number of exchanges.\n    <\/p>\n<h2>Input Format<\/h2>\n<p>\n        The first line contains the number of students N, and the next line contains N integers separated by spaces,<br \/>\n        representing the current positions of the students in line.\n    <\/p>\n<h3>Example Input<\/h3>\n<pre>\n5\n2 1 5 3 4\n    <\/pre>\n<h2>Output Format<\/h2>\n<p>\n        Output the minimum number of exchanges needed to sort in the correct order.\n    <\/p>\n<h3>Example Output<\/h3>\n<pre>\n3\n    <\/pre>\n<h2>Solution Process<\/h2>\n<p>\n        To solve this problem, we need to find a method to minimize position changes.<br \/>\n        It is important to identify the positions where exchanges are needed while checking if each student is in the order they should be.<br \/>\n        The basic approach is to use &#8220;cycle detection.&#8221;<br \/>\n        We trace each student, tracking the process of getting them into the correct position.\n    <\/p>\n<h3>Step 1: Cycle Analysis<\/h3>\n<p>\n        Initially, we calculate the correct position for each student using their numbers.<br \/>\n        For example, student 1 should be at index 0 (i=0).<br \/>\n        Student 2 should be at index 1 (j=1), and student 5 should be at index 4 (k=4).<br \/>\n        We check the given order to determine the direction in which swaps should occur to move to these correct positions.\n    <\/p>\n<h3>Step 2: Calculate Number of Swaps<\/h3>\n<p>\n        To ensure each student is in the correct position, we examine the length of the cycles.<br \/>\n        If the length of the cycle is k, then (k-1) swaps are needed.<br \/>\n        For example, if the cycle length is 3, 2 swaps are needed.<br \/>\n        Thus, summing the number of swaps for each cycle gives us the minimum number of swaps required.\n    <\/p>\n<h3>Step 3: Implement Swift Code<\/h3>\n<p>\n        Now, let&#8217;s write Swift code according to the algorithm described above.\n    <\/p>\n<pre><code>\n    func minimumSwaps(arr: [Int]) -> Int {\n        let n = arr.count\n        var indexedArr = Array(zip(arr, 0..<n)).sorted { $0.0 < $1.0 }\n        var visited = Array(repeating: false, count: n)\n        var swaps = 0\n        \n        for i in 0..<n {\n            if visited[i] || indexedArr[i].1 == i {\n                continue\n            }\n            \n            var cycle_size = 0\n            var j = i\n            \n            while !visited[j] {\n                visited[j] = true\n                j = indexedArr[j].1\n                cycle_size += 1\n            }\n            \n            if cycle_size > 0 {\n                swaps += (cycle_size - 1)\n            }\n        }\n        \n        return swaps\n    }\n    <\/code><\/pre>\n<h2>Example Execution<\/h2>\n<p>\n        Let\u2019s run the example using the above function:\n    <\/p>\n<pre><code>\n    let studentPositions = [2, 1, 5, 3, 4]\n    let result = minimumSwaps(arr: studentPositions)\n    print(\"Minimum number of swaps: \\(result)\")\n    <\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        &#8211; The <code>minimumSwaps<\/code> function takes the array of the students&#8217; current positions as input.<br \/>\n        &#8211; First, it pairs the students with their indices and sorts them based on their correct positions.<br \/>\n        &#8211; It declares an array for visited checking, examining the cycles at each student&#8217;s position,<br \/>\n        &#8211; counting the size of the cycles for students who are not in the correct positions.<br \/>\n        &#8211; Finally, it counts the necessary swaps for each cycle ((cycle size &#8211; 1)) and returns the total.\n    <\/p>\n<h2>Optimization and Additional Considerations<\/h2>\n<p>\n        This algorithm has a complexity of O(N log N) due to the sorting process, which can lead to significant performance degradation.<br \/>\n        However, if the number of students is not too large, this approach is sufficiently fast and efficient.<br \/>\n        While there are other methods to solve this problem, the cycle-based analysis is the most intuitive and straightforward.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        Today, through the queueing problem using Swift,<br \/>\n        we had the opportunity to enhance our algorithm problem-solving skills.<br \/>\n        The code is simple, but it combines multiple concepts, particularly the ideal process of cycle detection.<br \/>\n        I hope to build skills through more algorithms and problem-solving and be helpful in interview preparation!\n    <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today, as part of the Swift coding test course, we will address the queueing problem. This is one of the frequently encountered problems in coding interviews and is an algorithm that is widely used in actual work environments. In this post, I will explain the definition of the problem, the solution method, a Swift &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34846\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Swift Coding Test Course, Queueing&#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-34846","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, Queueing - \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\/34846\/\" \/>\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, Queueing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today, as part of the Swift coding test course, we will address the queueing problem. This is one of the frequently encountered problems in coding interviews and is an algorithm that is widely used in actual work environments. In this post, I will explain the definition of the problem, the solution method, a Swift &hellip; \ub354 \ubcf4\uae30 &quot;Swift Coding Test Course, Queueing&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34846\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:32:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:26:16+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34846\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34846\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Swift Coding Test Course, Queueing\",\"datePublished\":\"2024-11-01T09:32:40+00:00\",\"dateModified\":\"2024-11-01T11:26:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34846\/\"},\"wordCount\":593,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34846\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34846\/\",\"name\":\"Swift Coding Test Course, Queueing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:32:40+00:00\",\"dateModified\":\"2024-11-01T11:26:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34846\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34846\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34846\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift Coding Test Course, Queueing\"}]},{\"@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, Queueing - \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\/34846\/","og_locale":"ko_KR","og_type":"article","og_title":"Swift Coding Test Course, Queueing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today, as part of the Swift coding test course, we will address the queueing problem. This is one of the frequently encountered problems in coding interviews and is an algorithm that is widely used in actual work environments. In this post, I will explain the definition of the problem, the solution method, a Swift &hellip; \ub354 \ubcf4\uae30 \"Swift Coding Test Course, Queueing\"","og_url":"https:\/\/atmokpo.com\/w\/34846\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:32:40+00:00","article_modified_time":"2024-11-01T11:26:16+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34846\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34846\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Swift Coding Test Course, Queueing","datePublished":"2024-11-01T09:32:40+00:00","dateModified":"2024-11-01T11:26:16+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34846\/"},"wordCount":593,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34846\/","url":"https:\/\/atmokpo.com\/w\/34846\/","name":"Swift Coding Test Course, Queueing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:32:40+00:00","dateModified":"2024-11-01T11:26:16+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34846\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34846\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34846\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Swift Coding Test Course, Queueing"}]},{"@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\/34846","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=34846"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34846\/revisions"}],"predecessor-version":[{"id":34847,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34846\/revisions\/34847"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}