-
-
Notifications
You must be signed in to change notification settings - Fork 100
740. Delete and Earn #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DrDread746
wants to merge
3
commits into
SjxSubham:main
Choose a base branch
from
DrDread746:DrDread746-patch-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
740. Delete and Earn #255
Conversation
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
Reviewer's GuideThis 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 addedclassDiagram
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)
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]"]
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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);
};
Related Issues
Closes #254
By submitting this PR, I confirm that:
Summary by Sourcery
Add C++ solutions for LeetCode problems 3297, 3298, and 740
New Features: