Skip to content

Handle negative numbers in minimum subarray sum - #15848

Open
6iu8a wants to merge 1 commit into
TheAlgorithms:masterfrom
6iu8a:fix/shortest-signed-subarray
Open

6iu8a wants to merge 1 commit into
TheAlgorithms:masterfrom
6iu8a:fix/shortest-signed-subarray

Conversation

@6iu8a

@6iu8a 6iu8a commented Dec 3, 2026

Copy link
Copy Markdown

Describe your change

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

The positive-only sliding window misses valid answers on signed input. For example, minimum_subarray_sum(4, [-2, 4]) returns 0, and minimum_subarray_sum(3, [1, -1, 3]) returns 3. In each case the final value alone meets the target.

Track increasing prefix sums in a deque. Remove starts that already meet the target after recording their length, and discard starts that are dominated by a later prefix. Each index enters and leaves the deque at most once. The search runs in O(n) time and uses up to O(n) auxiliary space.

All 15 existing doctests pass. I compared the results to exhaustive enumeration for 234,372 combinations of signed lists and targets, including zero and negative targets. Ruff lint and formatting checks passed. AI-assisted 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. (No new files.)
  • 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 include at least one URL that points to Wikipedia or another similar explanation. (Existing algorithm.)
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword. (No matching open issue found.)

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant