Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Nov 23, 2025

Describe your change:

Finds the max runtime possible for running n computers given a list of batteries

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Added algorithm for computing maximum runtime across N computers using available batteries
  • Documentation

    • Added comprehensive algorithm documentation with problem description, solution approach, and usage examples
  • Tests

    • Added parameterized unit tests for maximum runtime calculation

✏️ Tip: You can customize this high-level summary in your review settings.

@BrianLusina BrianLusina self-assigned this Nov 23, 2025
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates Array Array data structure Binary Search Binary Search Algorithm labels Nov 23, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 23, 2025

Walkthrough

This PR introduces a complete binary search solution for determining the maximum runtime for N computers given a set of batteries. It includes documentation describing the problem and approach, a Python implementation with multiple algorithm variants, and parameterized unit tests validating the solutions against test cases.

Changes

Cohort / File(s) Summary
Documentation
DIRECTORY.md, algorithms/search/binary_search/maxruntime_n_computers/README.md
Adds a new directory entry for the max runtime problem and introduces comprehensive documentation covering the problem statement, binary search setup, feasibility checking logic, and complexity analysis with embedded visual examples.
Implementation
algorithms/search/binary_search/maxruntime_n_computers/__init__.py
Implements three public functions: max_runtime() and max_run_time_2() for binary search over feasible runtimes, and can_run_for() as a helper to check feasibility by validating if batteries can sustain target runtime for N computers.
Tests
algorithms/search/binary_search/maxruntime_n_computers/test_max_runtime.py
Adds parameterized unit test class with two test methods validating both max_runtime() and max_run_time_2() implementations across multiple input scenarios.

Sequence Diagram

sequenceDiagram
    participant Client
    participant max_runtime as max_runtime()
    participant Binary Search
    participant can_run_for as can_run_for()

    Client->>max_runtime: batteries, n
    activate max_runtime
    max_runtime->>Binary Search: left=0, right=sum(batteries)//n
    
    loop while left < right
        Binary Search->>Binary Search: mid = left + (right-left)//2
        Binary Search->>can_run_for: batteries, n, mid
        activate can_run_for
        can_run_for->>can_run_for: sum(min(battery, mid)) >= n*mid?
        alt Feasible (true)
            can_run_for-->>Binary Search: true
            Binary Search->>Binary Search: left = mid + 1
        else Not Feasible (false)
            can_run_for-->>Binary Search: false
            Binary Search->>Binary Search: right = mid
        end
        deactivate can_run_for
    end
    
    Binary Search-->>max_runtime: convergence
    max_runtime-->>Client: max_runtime value
    deactivate max_runtime
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • README.md: Documentation contains acknowledged typos ("rught", mismatched parentheses) that should be verified and corrected
  • init.py: Review both max_runtime() and max_run_time_2() implementations for algorithmic correctness, particularly the midpoint calculations and loop termination conditions
  • test_max_runtime.py: Verify test case coverage adequately validates edge cases (empty batteries, n=1, single battery, etc.)

Poem

🐰 With batteries lined up, computers in sight,
Binary search finds runtime so right!
Left hops and right hops, we search for the max,
Feasibility checked—no shortcuts to lack! ⚡

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding a binary search algorithm for calculating maximum runtime for n computers.
Description check ✅ Passed The PR description is well-structured and follows the template with all required sections completed and most checklist items properly marked.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/algorithms-max-runtime-for-n

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

🧹 Nitpick comments (4)
DIRECTORY.md (1)

96-97: New binary search entry looks consistent; consider md indentation to satisfy MD007.

The added “Maxruntime N Computers” entry and test link are aligned with existing structure and point to the correct test path. Markdownlint (MD007, ul-indent) is complaining about the indentation on these bullets, though, so if your CI enforces that rule you may want to tweak the leading spaces (or adjust the rule) to keep the linter green.

algorithms/search/binary_search/maxruntime_n_computers/README.md (1)

31-68: Tighten up typos and notation in the README.

The narrative matches the implemented algorithms, but there are a few small doc issues worth fixing:

  • Line 43: rughtright in the midpoint formula.
  • Line 46: “the target feasible” → “the target is feasible” (or similar).
  • Lines 62–64: clean up the extra closing parentheses in O(n⋅logT) and in T = sum(batteries) // n), and be consistent about what n denotes (number of computers vs number of batteries).
  • Line 68: osis, and consider ending with a period.

These fixes will make the explanation crisper and avoid confusion for readers following the math.

algorithms/search/binary_search/maxruntime_n_computers/test_max_runtime.py (1)

1-38: Solid parameterized coverage for both implementations; minor DRY opportunity.

The test cases and expected values look correct for the problem definition, and validating both max_runtime and max_run_time_2 against the same scenarios is a nice consistency check.

If you want to reduce duplication, you could extract the shared case list to a module-level constant and reuse it in both @parameterized.expand(...) decorators.

algorithms/search/binary_search/maxruntime_n_computers/__init__.py (1)

4-73: Binary search + feasibility check implementation looks correct; small reuse/clarity tweaks optional.

Both max_runtime and max_run_time_2 correctly:

  • Bound the search by sum(batteries) // n.
  • Use the monotone feasibility check sum(min(b, t)) >= n * t.
  • Handle the 0-minute baseline and converge to the maximum feasible runtime without off‑by‑one issues.

A couple of optional refinements you might consider:

  • Reuse can_run_for inside max_run_time_2 instead of duplicating the usable >= mid * n logic to keep the feasibility condition in one place.
  • If you care about micro-optimizations, you could short‑circuit the sum(min(...)) loops once the running total meets or exceeds n * target_time to avoid iterating the entire list for obviously feasible candidates.

Functionality-wise this is good to go.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b367a0b and 54c6e00.

⛔ Files ignored due to path filters (12)
  • algorithms/search/binary_search/maxruntime_n_computers/images/examples/max_runtime_n_computers_example_1.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/examples/max_runtime_n_computers_example_2.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/examples/max_runtime_n_computers_example_3.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_1.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_2.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_3.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_4.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_5.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_6.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_7.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_8.png is excluded by !**/*.png
  • algorithms/search/binary_search/maxruntime_n_computers/images/solution/max_runtime_n_computers_solution_9.png is excluded by !**/*.png
📒 Files selected for processing (4)
  • DIRECTORY.md (1 hunks)
  • algorithms/search/binary_search/maxruntime_n_computers/README.md (1 hunks)
  • algorithms/search/binary_search/maxruntime_n_computers/__init__.py (1 hunks)
  • algorithms/search/binary_search/maxruntime_n_computers/test_max_runtime.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
algorithms/search/binary_search/maxruntime_n_computers/test_max_runtime.py (1)
algorithms/search/binary_search/maxruntime_n_computers/__init__.py (2)
  • max_runtime (4-33)
  • max_run_time_2 (51-73)
🪛 LanguageTool
algorithms/search/binary_search/maxruntime_n_computers/README.md

[grammar] ~43-~43: Ensure spelling is correct
Context: ...2. While left < right: - Calculate mid = right - (rught - left) // 2 (biases the midpoint towa...

(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)

🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

96-96: Unordered list indentation
Expected: 4; Actual: 6

(MD007, ul-indent)


97-97: Unordered list indentation
Expected: 6; Actual: 8

(MD007, ul-indent)

@BrianLusina BrianLusina merged commit c09b702 into main Nov 23, 2025
5 of 6 checks passed
@BrianLusina BrianLusina deleted the feat/algorithms-max-runtime-for-n branch November 23, 2025 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Array Array data structure Binary Search Binary Search Algorithm Datastructures Datastructures Documentation Documentation Updates enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants