Skip to content
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

Use a set instead of a list, and a copy of it to count (i, j) and (j, i) once. #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ghost
Copy link

@ghost ghost commented Sep 22, 2021

This solution is merely for convenience and is neither memory nor time-efficient:

  • Lookups are O(n) in lists and O(1) in dictionaries and sets (because dictionaries and sets are implemented as hash tables). I used a set because we don't need to associate values (and also sets use less memory than dictionaries).

  • To count (i, j) and (j, i) once, I look up j in a copy of the set numbers (instead of the numbers itself), and then delete i from the copy. This way when i:=j, we won't find j:=i in the copy so it doesn't count. Also, we can't modify the set while we're iterating over it, and numbers have to reset for each time we call func(x) (these are other reasons I used a copy).

golabiesargashteh3 added 2 commits September 22, 2021 15:06
Lookups are O(n) in lists and O(1) in dictionaries and sets. I'll use a set because we don't need to associate values.
To count "(i, j)" and "(j, i)" once, we'll look up "j" in a copy of the set "numbers" (instead of the "numbers" itself), and then we'll delete "i" from the copy. This way when "i:=j", we won't find "j:=i" in the copy. Note that we can't modify the set while we're iterating over it, and also the numbers have to reset for each time we call "func(x)" (these are other reasons I use a copy of "numbers").
This solution is merely convenient and neither memory nor time-efficient.
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.

0 participants