Skip to content

Conversation

@DrDread746
Copy link
Contributor

@DrDread746 DrDread746 commented Oct 29, 2025

  1. Reschedule Meetings for Maximum Free Time I.cpp

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:

  • [✅] This is my original work not totally AI generated
  • [✅] I have tested the solution thoroughly on leetcode
  • [✅] I have maintained proper PR description format
  • [✅] This is a meaningful contribution, not spam

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:

  • Add sliding window solution for Count Substrings That Can Be Rearranged to Contain a String I using frequency maps
  • Add sliding window solution for Count Substrings That Can Be Rearranged to Contain a String II using frequency maps
  • Add solution for Reschedule Meetings for Maximum Free Time I by computing event gaps and applying a sliding window over k consecutive intervals

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 29, 2025

Reviewer's Guide

Adds 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 classes

classDiagram
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)
}
Loading

Flow diagram for gap-based sliding window in maxFreeTime

flowchart 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"]
Loading

File-Level Changes

Change Details Files
Introduce sliding window solution for substring rearrangement problems
  • Initialize frequency vectors for word2 and the current window in word1
  • Expand right pointer, update match count when frequencies align
  • Shrink window from left while valid and accumulate valid substring counts
3297. Count Substrings That Can Be Rearranged to Contain a String I.cpp
3298. Count Substrings That Can Be Rearranged to Contain a String II.cpp
Implement maxFreeTime using gap computation and sliding window
  • Construct gap array including pre-first and post-last event intervals
  • Use two-pointer window of size k to maintain running gap sum
  • Update maximum sum by adding new gap and removing the oldest
3439. Reschedule Meetings for Maximum Free Time I.cpp

Assessment against linked issues

Issue Objective Addressed Explanation
#252 Provide a working C++ solution for LeetCode problem 3439: Reschedule Meetings for Maximum Free Time I.
#252 Ensure the solution file is named according to the convention '[Number]. [Problem Title].cpp'.
#252 Include the Approach and Intuition sections in the PR description/documentation.

Possibly linked issues

  • #3439: The PR provides the C++ solution for 'Reschedule Meetings for Maximum Free Time I', which is exactly the problem described in issue 3439.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3439. Reschedule Meetings for Maximum Free Time I

1 participant