99클럽 코테 스터디 30일차 Find Right Interval

2024. 8. 21. 09:15·스터디/99클럽 코테 스터디 TIL

📝 문제

https://leetcode.com/problems/find-right-interval/description/

💦 풀이 실패

import java.util.*;

public class Solution {
    public int[] findRightInterval(int[][] intervals) {
        int n = intervals.length;
        
        // (start, index) 형태로 정렬하기 위해 start와 index를 저장하는 배열을 생성
        int[][] startPoints = new int[n][2];
        for (int i = 0; i < n; i++) {
            startPoints[i][0] = intervals[i][0];
            startPoints[i][1] = i;
        }
        
        // 시작점을 기준으로 정렬
        Arrays.sort(startPoints, Comparator.comparingInt(a -> a[0]));
        
        // 결과를 저장할 배열 초기화
        int[] result = new int[n];
        
        // 각 구간의 우측 구간을 찾기
        for (int i = 0; i < n; i++) {
            int end = intervals[i][1];
            
            // 이진 탐색으로 start >= end인 첫 번째 구간을 찾음
            int left = 0, right = n;
            while (left < right) {
                int mid = left + (right - left) / 2;
                if (startPoints[mid][0] >= end) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
            
            // 찾은 구간의 인덱스를 저장하거나, 없다면 -1 저장
            if (left < n) {
                result[i] = startPoints[left][1];
            } else {
                result[i] = -1;
            }
        }
        
        return result;
    }
}

 

'스터디 > 99클럽 코테 스터디 TIL' 카테고리의 다른 글

99클럽 코테 스터디 32일차 무인도 여행  (0) 2024.08.22
99클럽 코테 스터디 31일차 점프 점프  (0) 2024.08.22
99클럽 코테 스터디 29일차 Longest Increasing Subsequence  (0) 2024.08.19
99클럽 코테 스터디 28일차 괄호 회전하기  (0) 2024.08.19
99클럽 코테 스터디 27일차 할인 행사  (0) 2024.08.18
'스터디/99클럽 코테 스터디 TIL' 카테고리의 다른 글
  • 99클럽 코테 스터디 32일차 무인도 여행
  • 99클럽 코테 스터디 31일차 점프 점프
  • 99클럽 코테 스터디 29일차 Longest Increasing Subsequence
  • 99클럽 코테 스터디 28일차 괄호 회전하기
Been
Been
  • Been
    Been
    Been
  • 전체
    오늘
    어제
    • 분류 전체보기 (60)
      • 언어 (0)
        • Kotlin (0)
      • 안드로이드 (17)
      • iOS (3)
      • Git (1)
      • 스터디 (39)
        • 알고리즘 문제 풀이 (1)
        • 99클럽 코테 스터디 TIL (38)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    TIL
    java
    FragmentStateAdapter
    항해99
    풀이실패
    아이폰
    자바
    Androiod
    리싸이클러뷰
    Android
    maxWidth
    깃
    개발자취업
    IOS
    Git
    밑줄제거
    쓰기권한
    AndroidID
    99클럽
    EditText
    Coroutines
    WRITE EXTERNAL
    안드로이드
    debugRuntimeClasspath
    NSR
    객체변환
    코딩테스트준비
    nsl
    RecyclerView
    언더라인 제거
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Been
99클럽 코테 스터디 30일차 Find Right Interval
상단으로

티스토리툴바