Skip to content
  • PHP-Proxy

    PHP-Proxy

    Error accessing resource: 429 - Too Many Requests

  • Notifications You must be signed in to change notification settings
  • Fork 11

Time complexity of the algorithm #10

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

Closed
Eli-Black-Work opened this issue Apr 8, 2020 · 4 comments
Closed

Time complexity of the algorithm #10

Eli-Black-Work opened this issue Apr 8, 2020 · 4 comments

Comments

@Eli-Black-Work
Copy link

Would you be okay with adding something to the README stating what the time complexity of the algorithm is? My understanding from looking at the code is that it's O(n log n), but I'm not sure if that's correct.

Thanks :)

@sindresorhus
Copy link
Owner

Happy to document it, but I don't know the answer.

@Eli-Black-Work
Copy link
Author

Haha, okay, got it. Well, hopefully someone else who is algorithmically inclined will come along and see this :)

@wilsonwangme
Copy link

Haha, okay, got it. Well, hopefully someone else who is algorithmically inclined will come along and see this :)

If arrayOne's length is a and arrayTwo's length is b, isn't it the time complexity is O(ab)?

  1. We need at least iterate arrayOne a times and generally iterate arrayTwo b/2 times as an average situation.
  2. The iteration of arrayTwo is in each arrayOne's iteration, so the time complexity is O(a * b/2).
  3. The constant level factor should be ignored.
  4. As a result, the answer is O(ab)

@dotlambda
Copy link

If you call arrayDiffer(a, b) and m = a.length, n = b.length, then turning b into a set takes O(n * log n) because inserting a single element into the set takes O(n), and filtering a takes O(m * log n). To summarize, the complexity is O(max(m, n) * log n). But that's a worst-case analysis which might be irrelevant in real life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants