Skip to content

Conversation

@DrDread746
Copy link
Contributor

@DrDread746 DrDread746 commented Oct 29, 2025

  1. Delete and Earn.cpp

Intuition

The problem is similar to the "House Robber" problem but applied to numbers: if you take a number x, you cannot take x-1 or x+1.
The key insight is to transform the input array into a frequency array f[i], where f[i] counts how many times i appears. Now, instead of worrying about positions in the array, we only care about values. The maximum points we can earn for value i is f[i] * i, and we can use dynamic programming to decide whether to "take" or "skip" each number.

Approach

Build frequency array:
Count how many times each number appears in nums.
Keep track of the maximum number (maxVal) to limit DP size.

Dynamic Programming:
Let dp[i] be the maximum points we can earn considering numbers from 1 to i.
For each i ≥ 2, we have two options:
Skip i: dp[i] = dp[i-1]
Take i: dp[i] = dp[i-2] + f[i] * i (we skip i-1 because taking i deletes i-1)
Base case: dp[1] = f[1]

Return result:
dp[maxVal] is the maximum points we can earn.

Code Solution (C++)

class Solution {
public:
int deleteAndEarn(vector& nums) {
int m = 10001;
int n = nums.size();
int maxVal = 0;
vector f(m, 0);
vector dp(m + 1, 0);

    for (int i = 0; i < n; i++) {
        f[nums[i]]++;
        maxVal = max(maxVal, nums[i]);
    }

    dp[1] = f[1];
    for (int i = 2; i <= maxVal; i++) {
        dp[i] = max(dp[i - 1], dp[i - 2] + f[i] * i);
    }

    return dp[maxVal];
}

};

Related Issues

Closes #254

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 C++ solutions for LeetCode problems 3297, 3298, and 740

New Features:

  • Implement sliding window solutions for LeetCode 3297 and 3298 to count substrings of word1 that can be rearranged to contain word2
  • Implement frequency-based dynamic programming solution for LeetCode 740 (Delete and Earn)

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 29, 2025

Reviewer's Guide

This PR adds C++ solutions for three LeetCode problems: sliding window approach for substring counting (problems 3297 and 3298) and a dynamic programming solution for "Delete and Earn" (problem 740), transforming the latter into a frequency-based DP.

Class diagram for new Solution classes added

classDiagram
class Solution_3297 {
  +long long validSubstringCount(string word1, string word2)
}
class Solution_3298 {
  +long long validSubstringCount(string word1, string word2)
}
class Solution_740 {
  +int deleteAndEarn(vector<int>& nums)
}
Solution_3297 : validSubstringCount(word1, word2)
Solution_3298 : validSubstringCount(word1, word2)
Solution_740 : deleteAndEarn(nums)
Loading

Flow diagram for the dynamic programming approach in Delete and Earn (Problem 740)

flowchart TD
    A["Build frequency array f[i] from nums"] --> B["Find maxVal in nums"]
    B --> C["Initialize dp[1] = f[1]"]
    C --> D["For i = 2 to maxVal"]
    D --> E["dp[i] = max(dp[i-1], dp[i-2] + f[i]*i)"]
    E --> F["Return dp[maxVal]"]
Loading

File-Level Changes

Change Details Files
Added sliding window algorithm for counting valid substrings
  • Initialized frequency arrays for target pattern and current window
  • Used two-pointer expansion and contraction to maintain match count
  • Accumulated results by counting valid suffixes when full match is detected
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
Implemented dynamic programming solution for 'Delete and Earn' by converting inputs into a frequency-based problem
  • Built frequency array f and tracked maxVal from input nums
  • Initialized dp array with base case dp[1]
  • Computed dp[i] = max(dp[i-1], dp[i-2] + f[i] * i) iteratively
  • Returned dp[maxVal] as the maximum points
740. Delete and Earn.cpp

Assessment against linked issues

Issue Objective Addressed Explanation
#254 Provide a working C++ solution for LeetCode problem 740 (Delete and Earn) following the described dynamic programming approach.
#254 Include an Approach and Intuition section explaining the solution in the PR description.
#254 Ensure the filename follows the convention '[Number]. [Problem Title].cpp'.

Possibly linked issues

  • #740: The PR adds the exact C++ solution described in issue 740 for the 'Delete and Earn' problem.

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 and they look great!


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.

740. Delete and Earn

1 participant