feat(algorithms, top k elements): smallest range covering elements from k lists#172
feat(algorithms, top k elements): smallest range covering elements from k lists#172BrianLusina merged 4 commits intomainfrom
Conversation
…om k lists using a heap
…om k lists, two pointer and brute force
📝 WalkthroughWalkthroughAdds a new "Smallest Range Covering K Lists" problem under Top K Elements with a detailed README, three algorithm implementations (heap, two-pointer, brute-force), and unit tests verifying consistency across implementations. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@algorithms/top_k_elements/smallest_range_covering_k_lists/__init__.py`:
- Line 58: Typo in the inline comment: change the comment text "# sor the merged
list" to "# sort the merged list" so the comment correctly describes the
following operation (update the comment in __init__.py near the merged list
sorting code).
In `@algorithms/top_k_elements/smallest_range_covering_k_lists/README.md`:
- Line 196: Fix the typo in the README sentence that currently reads "So the
total tim complexity, in the worst case is `O(n log(k))` where n is the total
number of elements across all k lists" by changing "tim" to "time" so it reads
"So the total time complexity, in the worst case is `O(n log(k))` where n is the
total number of elements across all k lists".
🧹 Nitpick comments (2)
algorithms/top_k_elements/smallest_range_covering_k_lists/__init__.py (2)
8-9: Unnecessaryheapifycall on an empty list.
heapq.heapify([])is a no-op. Since you immediately useheappushin the loop below, this line can be removed.♻️ Suggested simplification
- min_heap: List[Tuple[int, int, int]] = [] - heapq.heapify(min_heap) + min_heap: List[Tuple[int, int, int]] = []
13-14: Return typeList[int]butrange_endis initialized tofloat("inf").
float("inf")is afloat, not anint. While the problem constraints guarantee valid input (so the range will always be updated before return), the type annotation-> List[int]is technically violated by the initial value. The same pattern appears insmallest_range_two_pointer(Line 64) andsmallest_range_brute_force(Line 93).Consider using
math.infwith an explicit cast or a large sentinel like10**9(given the constraint-10^3 <= nums[i][j] <= 10^3, a value likefloat("inf")works at runtime but misleads type checkers).Also applies to: 46-47
algorithms/top_k_elements/smallest_range_covering_k_lists/__init__.py
Outdated
Show resolved
Hide resolved
…it__.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@algorithms/top_k_elements/smallest_range_covering_k_lists/__init__.py`:
- Line 14: The sentinel float("inf") assigned to variables like range_end and
range_list[1] can leak a float into the declared List[int] return type for
degenerate inputs; add an early guard in the public functions that use these
sentinels (check the input variable nums and that no inner list is empty, e.g.,
in the functions that initialize range_end/range_list) and return an empty list
[] immediately when nums is empty or any inner list is empty; this preserves the
List[int] return type and prevents floats from being returned while leaving the
sentinel logic unchanged for normal cases.
🧹 Nitpick comments (2)
algorithms/top_k_elements/smallest_range_covering_k_lists/__init__.py (2)
8-9: Redundantheapifycall on an empty list.
heapq.heapify([])is a no-op. You can drop Line 9.♻️ Suggested fix
min_heap: List[Tuple[int, int, int]] = [] - heapq.heapify(min_heap)
1-2:DefaultDictfromtypingis deprecated since Python 3.9.You can annotate with
collections.defaultdictdirectly (e.g.,freq: defaultdict[int, int]) and drop theDefaultDictimport fromtyping. Low priority.
Describe your change:
Smallest range covering elements from k lists with 3 variations to the solution:
All the approaches have documentation attached for following along including tests
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
New Features
Documentation
Tests
Chores