{"id":33982,"date":"2024-11-01T09:22:43","date_gmt":"2024-11-01T09:22:43","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33982"},"modified":"2024-11-01T10:54:26","modified_gmt":"2024-11-01T10:54:26","slug":"c-coding-test-course-dividing-line-segments-into-groups","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33982\/","title":{"rendered":"C# Coding Test Course, Dividing Line Segments into Groups"},"content":{"rendered":"<p><body><\/p>\n<p>Algorithm problems are becoming increasingly important in coding tests, and many companies place great emphasis on the ability to solve these problems. In this course, we will address the issue of dividing given line segments into groups. This problem can be solved through efficient classification and sorting, as well as grouping logic.<\/p>\n<h2>Problem Description<\/h2>\n<p>When given line segments are represented as points on a map, write a program to divide these segments into overlapping segment groups. A segment has a starting point and an endpoint, and is provided in the following input format:<\/p>\n<div class=\"example\">\n<strong>Input Example:<\/strong><br \/>\n<code>5<\/code><br \/>\n<code>1 3<\/code><br \/>\n<code>2 5<\/code><br \/>\n<code>6 8<\/code><br \/>\n<code>7 9<\/code><br \/>\n<code>10 12<\/code>\n<\/div>\n<p>The first line of the above input indicates the number of segments, and the following lines represent the starting and ending points of each segment. Overlapping segments must be grouped together. For example, the input segments can be divided as follows:<\/p>\n<div class=\"note\">\n<strong>Output Example:<\/strong><br \/>\n<code>Group 1: (1, 5)<\/code><br \/>\n<code>Group 2: (6, 9)<\/code><br \/>\n<code>Group 3: (10, 12)<\/code>\n<\/div>\n<h2>Approach to Solve the Problem<\/h2>\n<p>To solve this problem, the following approach can be used:<\/p>\n<ol>\n<li>Store the line segments provided in the input in a list.<\/li>\n<li>Sort the list based on the starting points.<\/li>\n<li>Traverse the sorted list to group the overlapping segments.<\/li>\n<li>Finally, output the range of continuous segments for each group.<\/li>\n<\/ol>\n<h3>Step 1: Input Processing<\/h3>\n<p>First, we will define a structure to receive the segments input by the user and process the input. This structure will hold the starting point and endpoint of the segments, managing them in list form.<\/p>\n<pre><code>using System;\nusing System.Collections.Generic;\n\npublic class Segment{\n    public int Start { get; set; }\n    public int End { get; set; }\n    \n    public Segment(int start, int end){\n        Start = start;\n        End = end;\n    }\n}<\/code><\/pre>\n<h3>Step 2: Sorting<\/h3>\n<p>This is the code to sort the segment list based on starting points. This will bring similar segments closer together, preparing us to group them.<\/p>\n<pre><code>public List&lt;Segment&gt; SortSegments(List&lt;Segment&gt; segments){\n    segments.Sort((a, b) =&gt; a.Start.CompareTo(b.Start));\n    return segments;\n}<\/code><\/pre>\n<h3>Step 3: Grouping Logic<\/h3>\n<p>The grouping logic determines whether each segment can be added to the current group while traversing. If the starting point of the current segment is less than or equal to the endpoint of the last segment, it is included in the same group. Otherwise, a new group is started.<\/p>\n<pre><code>public List&lt;List&lt;Segment&gt;&gt; GroupSegments(List&lt;Segment&gt; segments){\n    List&lt;List&lt;Segment&gt;&gt; groups = new List&lt;List&lt;Segment&gt;&gt;();\n    groups.Add(new List&lt;Segment&gt;());\n    \n    foreach(var segment in segments){\n        var lastSegment = groups[groups.Count - 1];\n        if(lastSegment.Count == 0 || segment.Start &gt; lastSegment[lastSegment.Count - 1].End){\n            groups.Add(new List&lt;Segment&gt;());\n        }\n        groups[groups.Count - 1].Add(segment);\n    }\n\n    return groups;\n}<\/code><\/pre>\n<h3>Step 4: Final Output<\/h3>\n<p>Finally, we output the grouped results. The results are displayed in a simple text format based on the starting and ending points of each group.<\/p>\n<pre><code>public void PrintGroups(List&lt;List&lt;Segment&gt;&gt; groups){\n    for(int i = 0; i &lt; groups.Count; i++){\n        var group = groups[i];\n        if(group.Count &gt; 0){\n            Console.WriteLine($\"Group {i + 1}: ({group[0].Start}, {group[group.Count - 1].End})\");\n        }\n    }\n}<\/code><\/pre>\n<h2>Complete Code<\/h2>\n<p>Now let&#8217;s write the final code by combining all the steps:<\/p>\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\n\nclass Program\n{\n    public class Segment\n    {\n        public int Start { get; set; }\n        public int End { get; set; }\n\n        public Segment(int start, int end)\n        {\n            Start = start;\n            End = end;\n        }\n    }\n\n    public static void Main(string[] args)\n    {\n        int n = int.Parse(Console.ReadLine());\n        List&lt;Segment&gt; segments = new List&lt;Segment&gt;();\n\n        for (int i = 0; i &lt; n; i++)\n        {\n            var input = Console.ReadLine().Split(' ');\n            int start = int.Parse(input[0]);\n            int end = int.Parse(input[1]);\n            segments.Add(new Segment(start, end));\n        }\n\n        segments = SortSegments(segments);\n        var groups = GroupSegments(segments);\n        PrintGroups(groups);\n    }\n\n    public static List&lt;Segment&gt; SortSegments(List&lt;Segment&gt; segments)\n    {\n        segments.Sort((a, b) =&gt; a.Start.CompareTo(b.Start));\n        return segments;\n    }\n\n    public static List&lt;List&lt;Segment&gt;&gt; GroupSegments(List&lt;Segment&gt; segments)\n    {\n        List&lt;List&lt;Segment&gt;&gt; groups = new List&lt;List&lt;Segment&gt;&gt;();\n        groups.Add(new List&lt;Segment&gt;());\n\n        foreach (var segment in segments)\n        {\n            var lastSegment = groups[groups.Count - 1];\n            if (lastSegment.Count == 0 || segment.Start &gt; lastSegment[lastSegment.Count - 1].End)\n            {\n                groups.Add(new List&lt;Segment&gt;());\n            }\n            groups[groups.Count - 1].Add(segment);\n        }\n\n        return groups;\n    }\n\n    public static void PrintGroups(List&lt;List&lt;Segment&gt;&gt; groups)\n    {\n        for (int i = 0; i &lt; groups.Count; i++)\n        {\n            var group = groups[i];\n            if (group.Count &gt; 0)\n            {\n                Console.WriteLine($\"Group {i + 1}: ({group[0].Start}, {group[group.Count - 1].End})\");\n            }\n        }\n    }\n}<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Through this course, we explored how to solve the segment grouping problem and how to write code using C#. In coding tests, it is important to develop algorithmic thinking through a variety of problems. Practice different types of problems through repetitive exercises!<\/p>\n<h2>Additional Learning Resources<\/h2>\n<p>Here are some useful resources introduced during the process of solving this problem:<\/p>\n<ul>\n<li><a href=\"https:\/\/programmers.co.kr\/\" target=\"_blank\" rel=\"noopener\">Programmers<\/a>: A platform providing various algorithm problems.<\/li>\n<li><a href=\"https:\/\/leetcode.com\/\" target=\"_blank\" rel=\"noopener\">LeetCode<\/a>: A site providing domestic and international problems for algorithm practice.<\/li>\n<li><a href=\"https:\/\/www.acmicpc.net\/\" target=\"_blank\" rel=\"noopener\">Baekjoon Online Judge<\/a>: A site where you can solve algorithm problems of various difficulties.<\/li>\n<\/ul>\n<p>I hope this article helps in your coding test preparation. If you have any questions or additional inquiries, please leave a comment. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Algorithm problems are becoming increasingly important in coding tests, and many companies place great emphasis on the ability to solve these problems. In this course, we will address the issue of dividing given line segments into groups. This problem can be solved through efficient classification and sorting, as well as grouping logic. Problem Description When &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33982\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# Coding Test Course, Dividing Line Segments into Groups&#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":[90],"tags":[],"class_list":["post-33982","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# Coding Test Course, Dividing Line Segments into Groups - \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\/33982\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Coding Test Course, Dividing Line Segments into Groups - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Algorithm problems are becoming increasingly important in coding tests, and many companies place great emphasis on the ability to solve these problems. In this course, we will address the issue of dividing given line segments into groups. This problem can be solved through efficient classification and sorting, as well as grouping logic. Problem Description When &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Dividing Line Segments into Groups&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33982\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:22:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:54:26+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33982\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33982\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Dividing Line Segments into Groups\",\"datePublished\":\"2024-11-01T09:22:43+00:00\",\"dateModified\":\"2024-11-01T10:54:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33982\/\"},\"wordCount\":474,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33982\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33982\/\",\"name\":\"C# Coding Test Course, Dividing Line Segments into Groups - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:22:43+00:00\",\"dateModified\":\"2024-11-01T10:54:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33982\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33982\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33982\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Coding Test Course, Dividing Line Segments into Groups\"}]},{\"@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":"C# Coding Test Course, Dividing Line Segments into Groups - \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\/33982\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Dividing Line Segments into Groups - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Algorithm problems are becoming increasingly important in coding tests, and many companies place great emphasis on the ability to solve these problems. In this course, we will address the issue of dividing given line segments into groups. This problem can be solved through efficient classification and sorting, as well as grouping logic. Problem Description When &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Dividing Line Segments into Groups\"","og_url":"https:\/\/atmokpo.com\/w\/33982\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:22:43+00:00","article_modified_time":"2024-11-01T10:54:26+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33982\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33982\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Dividing Line Segments into Groups","datePublished":"2024-11-01T09:22:43+00:00","dateModified":"2024-11-01T10:54:26+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33982\/"},"wordCount":474,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33982\/","url":"https:\/\/atmokpo.com\/w\/33982\/","name":"C# Coding Test Course, Dividing Line Segments into Groups - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:22:43+00:00","dateModified":"2024-11-01T10:54:26+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33982\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33982\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33982\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# Coding Test Course, Dividing Line Segments into Groups"}]},{"@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\/33982","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=33982"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33982\/revisions"}],"predecessor-version":[{"id":33983,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33982\/revisions\/33983"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}