Skip to content

Comments

Refactor minimum deletions to use O(n) time#10

Merged
yamcodes merged 2 commits intomainfrom
9-improve-minimum-deletions-to-make-string-balanced-to-on-time-complexity
Feb 9, 2026
Merged

Refactor minimum deletions to use O(n) time#10
yamcodes merged 2 commits intomainfrom
9-improve-minimum-deletions-to-make-string-balanced-to-on-time-complexity

Conversation

@yamcodes
Copy link
Owner

@yamcodes yamcodes commented Feb 9, 2026

Summary by CodeRabbit

  • Performance Improvements

    • Optimized algorithm for the LeetCode problem reduces time complexity from O(n²) to O(n), with the original approach retained for reference.
  • Documentation

    • README updated with revised complexity metrics and improved in-code documentation/comments.
  • Chores

    • Added contributor metadata/comments to the repository for maintainability.

@yamcodes yamcodes linked an issue Feb 9, 2026 that may be closed by this pull request
@coderabbitai
Copy link

coderabbitai bot commented Feb 9, 2026

Caution

Review failed

The pull request is closed.

Walkthrough

This PR updates the README entry for LeetCode problem 1653 and replaces the solution implementation: it adds an optimized O(n) minimumDeletions(String) (with O(n) space) while preserving the original O(n²) approach as minimumDeletionsNaive(String), plus minor Javadoc and annotation edits.

Changes

Cohort / File(s) Summary
Documentation
README.md
Adjusted time/space complexities for problem 1653 and added contributor block metadata and suppression comment.
Algorithm Implementation
src/main/java/.../minimumdeletionstomakestringbalanced/Solution.java
Added optimized O(n) minimumDeletions(String) using prefix/suffix counts, introduced minimumDeletionsNaive(String) preserving the O(n²) approach, added @SuppressWarnings("unused") to getSliceInfo, and updated Javadoc/header text.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • PR #10: Same code-level changes — replaces naive O(n²) implementation with optimized O(n), adds minimumDeletionsNaive, and includes @SuppressWarnings on getSliceInfo.
  • PR #8: Directly related edits to the same Solution.java adding the naive getSliceInfo-based approach and adjustments around minimumDeletions.

Poem

🐰
From looping hills of n-squared night,
I hopped to linear morning light.
Prefix trails and suffix streams,
Balance found in tidy teams.
Hooray — a carrot for clean code! 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'Refactor minimum deletions to use O(n) time' directly and clearly describes the main change: optimizing the time complexity from O(n²) to O(n) for the minimum deletions solution.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 9-improve-minimum-deletions-to-make-string-balanced-to-on-time-complexity

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

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/java/codes/yam/leetcode/minimumdeletionstomakestringbalanced/Solution.java (1)

52-61: ⚠️ Potential issue | 🟡 Minor

Naive method returns Integer.MAX_VALUE for empty input.

If s is empty, minimumDeletionsNaive returns Integer.MAX_VALUE while minimumDeletions returns 0. This is likely a non-issue given LeetCode constraints (1 ≤ s.length), but worth noting the behavioral divergence if these methods are ever compared in tests.

🧹 Nitpick comments (1)
src/main/java/codes/yam/leetcode/minimumdeletionstomakestringbalanced/Solution.java (1)

76-106: Consider O(1) space single-pass variant.

The problem can be solved in O(n) time with O(1) space by maintaining a running bCount and tracking min(bCount + suffixA) in a single backward pass (or equivalently, a forward pass tracking bCount and computing min deletions on the fly). This would avoid the two auxiliary arrays.

Not a blocker — the current solution is clear and correct.

@yamcodes yamcodes merged commit 2528d5b into main Feb 9, 2026
1 of 2 checks passed
@yamcodes yamcodes deleted the 9-improve-minimum-deletions-to-make-string-balanced-to-on-time-complexity branch February 9, 2026 17:58
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.

Improve minimum-deletions-to-make-string-balanced to O(n) time complexity

1 participant