You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Your solutions are well-written and efficient. Here are some minor suggestions:
For Problem1, you can use collections.defaultdict(int) to simplify the code. Alternatively, you can use hm.get(r_sum, 0) to avoid the if-else block when updating the hash map. For example:
hm[r_sum] =hm.get(r_sum, 0) +1
This would make the code more concise.
In Problem1, the variable name comp is a bit ambiguous. Consider using complement or target for clarity.
For Problem2, the solution is correct and efficient. No changes are needed.
For Problem3, the solution is correct and efficient. However, you can also use a counter to count the frequency of each character and then calculate the number of pairs. But your current approach with a set is elegant.
Overall, great job! Your solutions are correct and optimal.
Your solutions are correct and efficient. Here are some minor suggestions for improvement:
For Problem1, you can use collections.defaultdict(int) to avoid the if-else when updating the hash map. However, your current method is acceptable.
Example:
fromcollectionsimportdefaultdicthm=defaultdict(int)
hm[0] =1# then in the loop:count+=hm[comp]
hm[r_sum] +=1
In Problem1, the variable comp is a common abbreviation for "complement", which is appropriate. However, ensure that your variable names are consistent and clear.
For Problem2, the solution is correct. Note that the problem requires the maximum length of contiguous subarray with equal number of 0s and 1s. Your approach is standard.
For Problem3, the solution is correct. You might consider using a counter to count frequencies and then iterate over the counts to determine the number of pairs and whether there is an odd count. However, your set-based approach is efficient and straightforward.
Generally, your code is clean and readable. Keep up the good work!
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
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.
No description provided.