- Notifications
You must be signed in to change notification settings - Fork 2
feat(heaps): maximal score after k operations #106
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
Conversation
WalkthroughAdded a new Heap puzzle "Maximal Score After K Operations": directory entry, README explaining the max-heap algorithm (pop max, add to score, push ceil(val/3) for k iterations), a Python implementation, and a nine-test unittest suite. Changes
Sequence Diagram(s)sequenceDiagram participant Caller participant max_score participant Heap Caller->>max_score: max_score(nums, k) max_score->>Heap: build max-heap (negate values) loop k iterations max_score->>Heap: pop max Heap-->>max_score: largest max_score->>max_score: score += largest max_score->>max_score: next = ceil(largest / 3) max_score->>Heap: push next end max_score-->>Caller: return score Estimated code review effort🎯 2 (Simple) | ⏱️ ~10–15 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (2)
puzzles/heap/maximal_score_after_k_operations/__init__.py (1)
9-11: Consider more idiomatic empty check.The current empty check works correctly, but Python idiom favors
if not nums:overlen(nums) == 0for checking empty sequences.Apply this diff for a more pythonic style:
# if there are no elements, just return the score of 0 -if len(nums) == 0: +if not nums: return scorepuzzles/heap/maximal_score_after_k_operations/test_maximal_score.py (1)
5-73: Comprehensive test coverage with good variety of scenarios.The test suite includes excellent coverage:
- Various array sizes (3 to 100+ elements)
- Different k values relative to array size
- Uniform values (test_8)
- Large-scale stress test (test_9 with k=1000)
All tests respect the problem constraints and validate the heap-based algorithm thoroughly.
Optional: Consider adding a test case where
ksignificantly exceedslen(nums)(e.g.,nums=[5,10],k=10) to explicitly verify the algorithm handles repeated operations on reduced values correctly.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (18)
puzzles/heap/maximal_score_after_k_operations/images/examples/maximal_score_after_k_operations_example_1.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/examples/maximal_score_after_k_operations_example_2.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/examples/maximal_score_after_k_operations_example_3.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_1.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_10.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_11.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_12.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_13.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_14.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_15.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_2.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_3.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_4.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_5.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_6.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_7.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_8.pngis excluded by!**/*.pngpuzzles/heap/maximal_score_after_k_operations/images/solution/maximal_score_after_k_operations_solution_9.pngis excluded by!**/*.png
📒 Files selected for processing (4)
DIRECTORY.md(1 hunks)puzzles/heap/maximal_score_after_k_operations/README.md(1 hunks)puzzles/heap/maximal_score_after_k_operations/__init__.py(1 hunks)puzzles/heap/maximal_score_after_k_operations/test_maximal_score.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
puzzles/heap/maximal_score_after_k_operations/test_maximal_score.py (1)
puzzles/heap/maximal_score_after_k_operations/__init__.py (1)
max_score(6-26)
🪛 LanguageTool
puzzles/heap/maximal_score_after_k_operations/README.md
[style] ~53-~53: Using many exclamation marks might seem excessive (in this case: 14 exclamation marks for a text that’s 3282 characters long)
Context: ...core_after_k_operations_solution_1.png)

🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md
522-522: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
523-523: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
🔇 Additional comments (1)
puzzles/heap/maximal_score_after_k_operations/__init__.py (1)
6-26: LGTM! Clean and efficient heap-based solution.The implementation correctly uses Python's
heapq(min-heap) with negation to simulate a max-heap, efficiently extracting the maximum value in each iteration and reinserting the reduced value. The algorithm matches the documented approach and achieves the expected O(k log n) time complexity.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (1)
puzzles/heap/maximal_score_after_k_operations/README.md (1)
20-21: Constraint line is now correct.The duplicate
≤symbol that was previously flagged has been resolved. Line 21 now correctly displays- 1 ≤ nums[i] ≤ 10^5.
🧹 Nitpick comments (1)
puzzles/heap/maximal_score_after_k_operations/README.md (1)
3-3: Consolidate the redundant problem statement.Lines 3 and 5–6 both describe the problem setup with slightly different wording. The first sentence in line 3 introduces the operation but then shifts abruptly to the examples. Consolidate these into a single, cohesive problem description to improve clarity and avoid repetition.
Apply this diff to restructure:
- You are given an array of integers nums and an integer k. You want to perform the following operation exactly k times: - - You are given a 0-indexed array of integer nums and an integer k. Your task is to maximize a score through a series of - operations. Initially, your score is set to 0 + You are given a 0-indexed array of integers `nums` and an integer `k`. Your task is to maximize a score by performing exactly k operations. Initially, your score is set to 0.Also applies to: 5-6
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
puzzles/heap/maximal_score_after_k_operations/README.md(1 hunks)
🧰 Additional context used
🪛 LanguageTool
puzzles/heap/maximal_score_after_k_operations/README.md
[style] ~53-~53: Using many exclamation marks might seem excessive (in this case: 14 exclamation marks for a text that’s 3281 characters long)
Context: ...core_after_k_operations_solution_1.png)

Describe your change:
Adds an algorithm to find the maximum score after applying k operations to a list of integers. This utilizes the max heap datastructure to ensure that the maximum value is readily available at the root of the max heap.
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
New Features
Documentation
Tests