Skip to content

Comments

Final Implementation#2160

Open
avcode3 wants to merge 1 commit intosuper30admin:masterfrom
avcode3:master
Open

Final Implementation#2160
avcode3 wants to merge 1 commit intosuper30admin:masterfrom
avcode3:master

Conversation

@avcode3
Copy link

@avcode3 avcode3 commented Feb 14, 2026

No description provided.

@super30admin
Copy link
Owner

Your solutions are well-written and efficient. Here are some minor suggestions:

  1. 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.

  2. In Problem1, the variable name comp is a bit ambiguous. Consider using complement or target for clarity.

  3. For Problem2, the solution is correct and efficient. No changes are needed.

  4. 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.

@super30admin
Copy link
Owner

Your solutions are correct and efficient. Here are some minor suggestions for improvement:

  1. 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:

    from collections import defaultdict
    hm = defaultdict(int)
    hm[0] = 1
    # then in the loop:
    count += hm[comp]
    hm[r_sum] += 1
  2. In Problem1, the variable comp is a common abbreviation for "complement", which is appropriate. However, ensure that your variable names are consistent and clear.

  3. 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.

  4. 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.

  5. Generally, your code is clean and readable. Keep up the good work!

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.

2 participants