# MeetingRoom

Given an array of meeting time intervals consisting of start and end times \[\[s1,e1],\[s2,e2],...] (si < ei), determine if a person could attend all meetings.

> Example 1:\
> Input : \[\[0,30]\[5,10],\[15,20]]\
> Output : false
>
> Example 2 :\
> Input : \[\[7,10],\[2,4]]\
> Output : true

**Solution**\
1\. 시작 시간 기준으로 **Sorting**.\
2\. 이전의 끝 > 다음의 시작 **Compare**.

**의사코드->Java 언어로 구현**

```java
//Interval 클래스 구현
class Interval {
    int start;
    int end;
    //constructor(생성자)로 초기화
    Interval() {
        this.start = 0;
        this.end = 0;
    }
    Interval(int s, int e) {
        this.start = s;
        this.end = e;
    }
}
//Solution 시작
public class MeetingRoom {
    public static void main(String[] args) {
        MeetingRoom a = new MeetingRoom();
        Interval in1 = new Interval(0,30);
        Interval in2 = new Interval(10,15);
        Interval in3 = new Interval(20,25);
        
        Interval[] intervals = {in1,in2,in3};
        System.out.println(a.solve(intervals));
    }
    public boolean solve(Interval[] intervals) {
        if(intervals == null)
            return false;
        print(Intervals);
        //1.시작 시간 기준으로 Sorting.
        Arrays.sort(intervals, Comp);
        //2.이전의 끝 > 다음의 시작 Compare.
        for(int i=1; i<intervals.length;i++) {
            if(intervals[i-1].end > intervals[i].start)
                return false;
        }
        return true;
    }
    Comparator<Interval> Comp = new Comparator<Interval>(){
        @Override
        public int compare(Interval o1, Interval o2) {
            return o1.start - o2.start;
        }
    };
    public void print(Interval[] intervals) {
        for(int i = 0; i<intervals.length; i++) {
            Interval in = intervals[i];
            System.out.println(in.start + "  " + in.end);
        }
        
    }
}
```

**Java 복습**

Compartor와 Comparable\
\- Comparable : 기본 정렬기준을 구현하는 데 사용\
\- **Comparator** : 기본 정렬기준 외에 다른 기준으로 정렬하고자 할 때 사용

* 오름차순 정렬\
  1\. Comparator 오버라이드

```java
Comparator<Interval> Comp = new Comparator<Interval>(){
    @Override
    public int compare(Interval o1, Interval o2) {
        return o1.start - o2.start;
    }
};
```

2\. Comparator 안에서 움직이는 것은 compareTo 메소드이다.

```java
//compareTo 작동원리 이해.오른쪽이 크면 -1
public void compareToTest() {
    Integer num = 5;
    System.out.println(num.compareTo(3));//1
    System.out.println(num.compareTo(5));//0
    System.out.println(num.compareTo(88));//-1
}
```

3\. 람다식을 이용한 오름차순 정렬.&#x20;

**Arrays.sort(array, 람다식)**\
람다식 : **(o1,o2)->o1.start-o2.start**

```java
Arrays.sort(intervals,(o1,o2)->o1.start-o2.start);
```


---

# Agent Instructions: 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:

```
GET https://heunnajo.gitbook.io/algorithms-problem-solving-skills/algorithm-problems/meetingroom.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
