[JAVA 문제 풀이] 372. Reschedule Meetings for Maximum Free Time I [최대 여유 시간을 위해 회의 일정 변경 I]

Stupefyee's avatar
Jul 09, 2025
[JAVA 문제 풀이] 372. Reschedule Meetings for Maximum Free Time I [최대 여유 시간을 위해 회의 일정 변경 I]
Reschedule Meetings for Maximum Free Time I - LeetCode
Can you solve this real interview question? Reschedule Meetings for Maximum Free Time I - You are given an integer eventTime denoting the duration of an event, where the event occurs from time t = 0 to time t = eventTime. You are also given two integer arrays startTime and endTime, each of length n. These represent the start and end time of n non-overlapping meetings, where the ith meeting occurs during the time [startTime[i], endTime[i]]. You can reschedule at most k meetings by moving their start time while maintaining the same duration, to maximize the longest continuous period of free time during the event. The relative order of all the meetings should stay the same and they should remain non-overlapping. Return the maximum amount of free time possible after rearranging the meetings. Note that the meetings can not be rescheduled to a time outside the event.   Example 1: Input: eventTime = 5, k = 1, startTime = [1,3], endTime = [2,5] Output: 2 Explanation: [https://assets.leetcode.com/uploads/2024/12/21/example0_rescheduled.png] Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2]. Example 2: Input: eventTime = 10, k = 1, startTime = [0,2,9], endTime = [1,4,10] Output: 6 Explanation: [https://assets.leetcode.com/uploads/2024/12/21/example1_rescheduled.png] Reschedule the meeting at [2, 4] to [1, 3], leaving no meetings during the time [3, 9]. Example 3: Input: eventTime = 5, k = 2, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5] Output: 0 Explanation: There is no time during the event not occupied by meetings.   Constraints: * 1 <= eventTime <= 109 * n == startTime.length == endTime.length * 2 <= n <= 105 * 1 <= k <= n * 0 <= startTime[i] < endTime[i] <= eventTime * endTime[i] <= startTime[i + 1] where i lies in the range [0, n - 2].
Reschedule Meetings for Maximum Free Time I - LeetCode
notion image
notion image
이벤트 기간을 나타내는 정수 eventTime이 주어지며, 여기서 이벤트는 시간 t = 0에서 시간 t = eventTime까지 발생합니다. 또한 두 개의 정수 배열인 startTime과 endTime이 주어지며, 각각의 길이는 n입니다. 이는 겹치지 않는 n개의 회의의 시작 시간과 종료 시간을 나타내며, 여기서 i번째 회의는 [startTime[i], endTime[i]] 시간 동안 발생합니다. 이벤트 기간 동안 가장 긴 연속 자유 시간을 최대화하기 위해 시작 시간을 동일한 기간으로 이동하여 대부분의 k회의 일정을 변경할 수 있습니다. 모든 회의의 상대적 순서는 동일하게 유지되어야 하며 겹치지 않아야 합니다. 회의를 재조정한 후 가능한 최대 여유 시간을 반환하세요. 회의 일정을 행사 외의 시간으로 변경할 수 없음을 유의하세요. 제약 조건: * 1 <= eventTime <= 109 * n == startTime.length == endTime.length * 2 <= n <= 105 * 1 <= k <= n * 0 <= startTime[i] < endTime[i] <= eventTime * endTime[i] <= startTime[i + 1] 여기서 i는 [0, n - 2] 범위에 있습니다.
 

내가 작성한 코드 + 다른 사람의 코드

import java.util.*; class Solution { public int maxFreeTime(int eventTime, int k, int[] startTime, int[] endTime) { // 각 이벤트 사이사이, 맨 앞/맨 뒤 자유시간을 계산 int[] gaps = getGaps(eventTime, startTime, endTime); // gaps[i]: 회의 간 자유시간 (총 n+1개) // 초기 슬라이딩 윈도우: k+1개의 자유시간 합 int windowSum = Arrays.stream(gaps, 0, k + 1).sum(); int ans = windowSum; // 최댓값 초기화 // 슬라이딩 윈도우 이동 for (int i = k + 1; i < gaps.length; i++) { windowSum += gaps[i] - gaps[i - k - 1]; // 맨 앞 빼고 맨 뒤 더함 ans = Math.max(ans, windowSum); // 최대 자유시간 갱신 } return ans; } // 이벤트 앞, 뒤, 사이 자유시간 배열을 계산하는 메서드 private int[] getGaps(int eventTime, int[] startTime, int[] endTime) { int[] gaps = new int[startTime.length + 1]; // 자유시간은 n+1개 // 이벤트 시작 전 자유시간 gaps[0] = startTime[0]; // 각 회의 사이의 자유시간 for (int i = 1; i < startTime.length; ++i) { gaps[i] = startTime[i] - endTime[i - 1]; } // 이벤트 끝난 후 자유시간 gaps[startTime.length] = eventTime - endTime[endTime.length - 1]; return gaps; } }
Share article

stupefyee