> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2023/12-2023-dec-21/22-maximum-meetings-in-one-room.md).

# 22. Maximum Meetings in One Room

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/maximum-meetings-in-one-room/1)

![](https://badgen.net/badge/Level/Medium/yellow)

## My Approach

This is a greedy problem, to solve this problem:

* Firstly I creating a structure `meet` to represent meeting details, including start time, end time, and index.
* Then, I sort the meetings based on their finish times using a comparison function `comp`.
* After sorting, I iterate through the sorted list of meetings, selecting each meeting only if its start time is strictly greater than the finish time of the previous meeting which I stored in `last` variable. This ensures that the selected meetings do not overlap, allowing us to manage as many meetings as possible while maximizing the non-overlapping intervals.
* The final step involves returning the indices of the selected meetings.

## Time and Auxiliary Space Complexity

* **Time Complexity**: `O(N log N)` for sorting, where N is the number of meetings.
* **Auxiliary Space Complexity**: `O(N)` for the `v` vector.

## Code (C++)

```cpp
class Solution {
public:
    struct meet {
        int start, end, index;
    };

    static bool comp(struct meet& a, struct meet& b) {
        if (a.end == b.end)
            return a.start < b.start;

        return a.end < b.end;
    }

    vector<int> maxMeetings(int N, vector<int>& S, vector<int>& F) {
        vector<meet> v;
        vector<int> out;

        for (int i = 0; i < N; ++i)
            v.push_back({S[i], F[i], i + 1});

        sort(v.begin(), v.end(), comp);

        int last = 0;
        for (auto i : v)
            if (last < i.start) {
                last = i.end;
                out.push_back(i.index);
            }

        sort(out.begin(), out.end());
        return out;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2023/12-2023-dec-21/22-maximum-meetings-in-one-room.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
