코린이의 소소한 공부노트

[LeetCode/Easy] 2446. Determine if Two Events Have Conflict 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 2446. Determine if Two Events Have Conflict

무지맘 2023. 5. 4. 14:45

1. Input

1) String[] event1

2) String[] event2

 

2. Output

1) 두 행사의 시간이 겹치면 true, 겹치지 않으면 false를 반환

 

3. Constraint

1) evnet1.length == event2.length == 2.

2) event1[i].length == event2[i].length == 5

3) startTime1 <= endTime1

4) startTime2 <= endTime2

5) 모든 시간은 유효한 HH:MM 형식으로 되어있다.

 

4. Example

Input: event1 = ["01:15","02:00"], event2 = ["02:00","03:00"] -> Output: true

Input: event1 = ["01:00","02:00"], event2 = ["01:20","03:00"] -> Output: true

Input: event1 = ["10:00","11:00"], event2 = ["14:00","15:00"] -> Output: false

 

5. Code

1) 첫 코드(2023/05/04)

class Solution {
    public boolean haveConflict(String[] event1, String[] event2) {
        int[] e1 = new int[2], e2 = new int[2];
        for(int i=0 ; i<2 ; i++){
            e1[i] = Integer.valueOf(event1[i].substring(0,2))*60 + Integer.valueOf(event1[i].substring(3,5));
            e2[i] = Integer.valueOf(event2[i].substring(0,2))*60 + Integer.valueOf(event2[i].substring(3,5));
        }
        return (e1[0]<=e2[0] && e2[0]<=e1[1]) || (e2[0]<=e1[0] && e1[0]<=e2[1]);
    }
}