{"id":955,"date":"2025-08-21T18:16:46","date_gmt":"2025-08-21T12:46:46","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=955"},"modified":"2025-08-21T18:16:47","modified_gmt":"2025-08-21T12:46:47","slug":"n-meetings-in-one-room","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/n-meetings-in-one-room\/","title":{"rendered":"N meetings in one room | Greedy Solution"},"content":{"rendered":"\n<p>The <strong>N Meetings in One Room<\/strong> problem is a classic example of the <strong>Greedy Algorithm<\/strong>. It focuses on scheduling tasks in such a way that the maximum number of non-overlapping activities can take place. This problem is very common in coding interviews and is closely related to the <strong>Activity Selection Problem<\/strong>.<\/p>\n\n\n\n<p>In this blog, we will understand the problem, explore the greedy approach that guarantees the optimal result, and go through the Python implementation step by step.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/n-meetings-in-one-room-1587115620\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Problem Statement \u2013 What Does the Problem Say?<\/h2>\n\n\n\n<p>You are given two arrays:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>start[]<\/strong> \u2013 the starting times of meetings<\/li>\n\n\n\n<li><strong>end[]<\/strong> \u2013 the ending times of meetings<\/li>\n<\/ul>\n\n\n\n<p>Your task is to find the <strong>maximum number of meetings<\/strong> that can be scheduled in a single room such that no two meetings overlap.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Input: \nstart = [1, 3, 0, 5, 8, 5] \nend   = [2, 4, 6, 7, 9, 9]\n\nOutput: 4\nExplanation:\nOne of the possible selections is meetings at:\n(1,2), (3,4), (5,7), (8,9)\nSo, maximum 4 meetings can be held in the room.\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #DCDCAA\">Input<\/span><span style=\"color: #D4D4D4\">: <\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">start<\/span><span style=\"color: #D4D4D4\"> = [1, 3, 0, 5, 8, 5] <\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">   = [2, 4, 6, 7, 9, 9]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">Output<\/span><span style=\"color: #D4D4D4\">: 4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">Explanation<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">One<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">of<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">the<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">possible<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">selections<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">meetings<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">at<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">(1,2), (3,4), (5,7), (8,9)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">So, maximum 4 meetings can be held in the room.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Input:\nstart = [10, 12, 20]\nend   = [20, 25, 30]\n\nOutput: 2\nExplanation:\nWe can schedule meetings (10,20) and (20,30).\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #DCDCAA\">Input<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">start<\/span><span style=\"color: #D4D4D4\"> = [10, 12, 20]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">   = [20, 25, 30]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">Output<\/span><span style=\"color: #D4D4D4\">: 2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">Explanation<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">We can schedule meetings (10,20) and (20,30).<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The problem essentially asks: <strong>How can we schedule the maximum number of non-overlapping meetings in one room?<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Intuition and Approach (Optimal \u2013 Greedy)<\/h2>\n\n\n\n<p>The greedy approach works perfectly here because the best strategy is always to select the meeting that <strong>finishes earliest<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does this work?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If a meeting ends early, the room becomes available sooner, allowing more meetings to fit in.<\/li>\n\n\n\n<li>By always picking the meeting with the <strong>smallest finishing time<\/strong> that does not overlap with previously chosen meetings, we maximize the number of scheduled meetings.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to Solve:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Pair each meeting with its start and end time.<\/li>\n\n\n\n<li>Sort all meetings based on their <strong>end time<\/strong> (if two meetings have the same end time, sort by start time).<\/li>\n\n\n\n<li>Select the first meeting and keep track of its end time.<\/li>\n\n\n\n<li>For every other meeting:\n<ul class=\"wp-block-list\">\n<li>If its start time is greater than the end time of the last selected meeting, select it.<\/li>\n\n\n\n<li>Update the end time to the current meeting\u2019s end time.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Continue until all meetings are checked.<\/li>\n<\/ol>\n\n\n\n<p>This greedy strategy ensures that we always make the most optimal choice at each step.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Implementation<\/h2>\n\n\n\n<p>Here\u2019s the Python code for the <strong>N Meetings in One Room<\/strong> problem using the greedy method (with added comments for clarity):<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Meeting:\n    def __init__(self, start, end, position):\n        self.start = start\n        self.end = end\n        self.position = position\n\n\nclass Solution:\n    def maximumMeetings(self, start, end):\n        n = len(start)\n        # Create Meeting objects with start, end, and position\n        meets = [Meeting(start[i], end[i], i + 1) for i in range(n)]\n        \n        # Sort meetings by end time (then by start time if needed)\n        meets.sort(key=lambda x: (x.end, x.start))\n        \n        lastTime = meets[0].end\n        count = 1  # Select the first meeting by default\n        \n        # Iterate through the rest of the meetings\n        for i in range(1, n):\n            if meets[i].start &gt; lastTime:\n                count += 1\n                lastTime = meets[i].end\n                \n        return count\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Meeting<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">start<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">position<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.start = start<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.end = end<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.position = position<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">maximumMeetings<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">start<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(start)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Create Meeting objects with start, end, and position<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        meets = [Meeting(start[i], end[i], i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Sort meetings by end time (then by start time if needed)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        meets.sort(<\/span><span style=\"color: #9CDCFE\">key<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">lambda<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">: (x.end, x.start))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        lastTime = meets[<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">].end<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        count = <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Select the first meeting by default<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Iterate through the rest of the meetings<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> meets[i].start &gt; lastTime:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                count += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                lastTime = meets[i].end<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> count<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, each meeting is represented by a <code>Meeting<\/code> object storing its start time, end time, and position.<\/li>\n\n\n\n<li>The list of meetings is sorted by end times. This ensures that the earliest finishing meeting is always considered first.<\/li>\n\n\n\n<li>The algorithm starts by picking the first meeting.<\/li>\n\n\n\n<li>For each subsequent meeting, we check if its start time is greater than the end time of the last chosen meeting.\n<ul class=\"wp-block-list\">\n<li>If yes, we select it and update the last end time.<\/li>\n\n\n\n<li>If no, we skip it to avoid overlap.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>The variable <code>count<\/code> keeps track of how many meetings can be scheduled.<\/li>\n<\/ul>\n\n\n\n<p>For example, if <code>start = [1,3,0,5,8,5]<\/code> and <code>end = [2,4,6,7,9,9]<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sorted by end times \u2192 [(1,2), (3,4), (0,6), (5,7), (8,9), (5,9)]<\/li>\n\n\n\n<li>Select (1,2) \u2192 last end = 2<\/li>\n\n\n\n<li>Next, (3,4) \u2192 start 3 > 2, so select \u2192 last end = 4<\/li>\n\n\n\n<li>Next, (0,6) \u2192 start 0 \u2264 4, skip<\/li>\n\n\n\n<li>Next, (5,7) \u2192 start 5 > 4, so select \u2192 last end = 7<\/li>\n\n\n\n<li>Next, (8,9) \u2192 start 8 > 7, so select \u2192 last end = 9<\/li>\n\n\n\n<li>Total meetings = 4<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Time and Space Complexity<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Creating Meeting objects: <strong>O(n)<\/strong><\/li>\n\n\n\n<li>Sorting meetings: <strong>O(n log n)<\/strong><\/li>\n\n\n\n<li>Iterating through meetings: <strong>O(n)<\/strong><\/li>\n\n\n\n<li><strong>Overall: O(n log n)<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Extra space for storing meetings list: <strong>O(n)<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>This makes the approach efficient and suitable for large inputs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <strong>N Meetings in One Room<\/strong> problem is a direct application of the <strong>Greedy Algorithm<\/strong>. By always choosing the meeting that finishes earliest, we maximize the total number of non-overlapping meetings that can be scheduled.<\/p>\n\n\n\n<p>The approach runs in <strong>O(n log n)<\/strong> time due to sorting and uses <strong>O(n)<\/strong> space, making it both efficient and optimal.<\/p>\n\n\n\n<p>This problem is an excellent example of how greedy strategies can be applied effectively in scheduling and resource allocation problems.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The N Meetings in One Room problem is a classic example of the Greedy Algorithm. It focuses on scheduling tasks in such a way that the maximum number of non-overlapping activities can take place. This problem is very common in coding interviews and is closely related to the Activity Selection Problem. In this blog, we<\/p>\n","protected":false},"author":1,"featured_media":956,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[41,19],"class_list":{"0":"post-955","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-greedy-algorithm","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/08\/N-meetings-in-one-room-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=955"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"predecessor-version":[{"id":957,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/955\/revisions\/957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/956"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}