3439. Reschedule Meetings for Maximum Free Time I#253
Open
DrDread746 wants to merge 3 commits intoSjxSubham:mainfrom
Open
3439. Reschedule Meetings for Maximum Free Time I#253DrDread746 wants to merge 3 commits intoSjxSubham:mainfrom
DrDread746 wants to merge 3 commits intoSjxSubham:mainfrom
Conversation
Reviewer's GuideAdds sliding window solutions for substring rearrangement problems and a gap-based sliding window algorithm to compute maximum free time across k consecutive gaps in meeting schedules. Class diagram for new Solution classesclassDiagram
class Solution_3297 {
+long long validSubstringCount(string word1, string word2)
}
class Solution_3298 {
+long long validSubstringCount(string word1, string word2)
}
class Solution_3439 {
+int maxFreeTime(int eventTime, int k, vector<int>& startTime, vector<int>& endTime)
}
Flow diagram for gap-based sliding window in maxFreeTimeflowchart TD
A["Input: eventTime, k, startTime[], endTime[]"] --> B["Compute gaps between events (temp[])"]
B --> C["Initialize sliding window pointers l, r"]
C --> D["Iterate r over temp[]"]
D --> E["Add temp[r] to tempsum"]
E --> F["If window size == k"]
F --> G["Update maxsum if tempsum > maxsum"]
G --> H["Subtract temp[l] from tempsum and increment l"]
H --> D
D -->|r reaches end| I["Return maxsum"]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- This PR combines three unrelated problem solutions—please split it into separate PRs so each submission remains focused and easier to review.
- In maxFreeTime(), the sliding‐window check
if ((r - l) == k)currently sums k+1 gaps; double‐check the window boundaries so you only include exactly k gaps per window. - The two validSubstringCount implementations are identical duplicates; consider extracting the logic into a shared helper or common function to reduce repetition.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- This PR combines three unrelated problem solutions—please split it into separate PRs so each submission remains focused and easier to review.
- In maxFreeTime(), the sliding‐window check `if ((r - l) == k)` currently sums k+1 gaps; double‐check the window boundaries so you only include exactly k gaps per window.
- The two validSubstringCount implementations are identical duplicates; consider extracting the logic into a shared helper or common function to reduce repetition.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intuition
We are asked to find the maximum total free time across k consecutive gaps between events.
The key idea is to first compute all gaps between consecutive events (including the gap before the first event and after the last event). Once we have these gaps, the problem reduces to finding the maximum sum of any k consecutive gaps, which can be efficiently done using a sliding window.
Approach
Compute gaps:
The first gap is the time from 0 to the start of the first event.
The last gap is the time from the end of the last event to eventTime.
For all intermediate gaps, calculate startTime[i] - endTime[i-1].
Sliding window to find maximum sum:
Use two pointers l (left) and r (right) to maintain a window of size k.
Keep a running sum of the gaps in the current window.
As the window moves forward, update the sum by adding the new gap and removing the leftmost gap.
Track the maximum sum seen so far.
Return the result:
After processing all windows, the maximum sum represents the largest total free time for k consecutive gaps.
Code Solution (C++)
class Solution {
public:
int maxFreeTime(int eventTime, int k, vector& startTime, vector& endTime) {
int n = startTime.size();
vector temp(n + 1, 0);
temp[0] = startTime[0] - 0;
temp[n] = eventTime - endTime[n - 1];
for (int i = 1; i < n; i++) {
temp[i] = startTime[i] - endTime[i - 1];
}
int l = 0, r = 0, tempsum = 0, maxsum = 0;
while (r < temp.size()) {
tempsum += temp[r];
if ((r - l) == k) {
maxsum = max(maxsum, tempsum);
tempsum -= temp[l];
l++;
}
r++;
}
return maxsum;
}
};
Related Issues
Closes #252
By submitting this PR, I confirm that:
Summary by Sourcery
Add three new C++ solutions for LeetCode problems: two substring rearrangement count variants and a meeting rescheduling free-time maximization using sliding window methods.
New Features: