Skip to content

Conversation

@allrob23
Copy link

@allrob23 allrob23 commented Mar 2, 2025

Description

This PR optimizes the extend_safe function by replacing linear searches with constant-time set lookups, reducing time complexity from O(n*m) to O(n+m).

Changes

  • Modified extend_safe to use a set for checking element existence
  • Maintain a set copy of the target list to enable O(1) lookups
  • Update the set as new elements are added to keep it in sync

Performance Impact

  • Before: O(n*m) where n = len(target) and m = len(source)
  • After: O(n+m) with constant-time membership checks

Testing

  • Tested with various input sizes to verify correctness and performance improvement
  • Maintains identical functionality while significantly improving execution time for large lists

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.

1 participant