-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Create first_missing_positive.py #11186
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
Closed
moaldeen
wants to merge
9
commits into
TheAlgorithms:master
from
moaldeen:moaldeen-first-missing-positive
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
46b7f4a
Create first_missing_positive.py
moaldeen a838180
Update first_missing_positive.py
moaldeen 57a13c5
Update first_missing_positive.py
moaldeen 5ca2dc4
Update first_missing_positive.py
moaldeen 53c3617
Update first_missing_positive.py
moaldeen 7a28267
Update first_missing_positive.py
moaldeen 6097a40
Update data_structures/arrays/first_missing_positive.py
moaldeen 98cc485
Update data_structures/arrays/first_missing_positive.py
moaldeen 059de56
Update data_structures/arrays/first_missing_positive.py
moaldeen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
def first_missing_positive(nums: list[int]) -> int: | ||
""" | ||
Given an unsorted integer array nums, returns the smallest missing positive integer. | ||
|
||
The algorithm must run in O(n) time and use O(1) auxiliary space. | ||
|
||
Hints: | ||
- Try to place each number in its correct position, so that nums[i] equals i + 1. | ||
- Iterate through the array to find the first position where nums[i] != i + 1, | ||
indicating the missing positive integer. | ||
|
||
:param nums: The input unsorted integer array. | ||
:return: The smallest missing positive integer. | ||
|
||
Examples: | ||
>>> first_missing_positive([1, 2, 0]) | ||
3 | ||
|
||
>>> first_missing_positive([3, 4, -1, 1]) | ||
2 | ||
|
||
>>> first_missing_positive([7, 8, 9, 11, 12]) | ||
1 | ||
|
||
>>> first_missing_positive([]) | ||
1 | ||
|
||
>>> first_missing_positive([0]) | ||
1 | ||
|
||
>>> first_missing_positive([1]) | ||
2 | ||
|
||
>>> first_missing_positive([0.9, 1.5, -2.3, 3.0, 2.7]) | ||
1 | ||
|
||
>>> first_missing_positive([-1, -2, -3]) | ||
1 | ||
|
||
>>> first_missing_positive([1.5, -2.3, 3.0, 2.7]) | ||
1 | ||
|
||
>>> first_missing_positive("ABC") | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Input must be a list of numbers | ||
""" | ||
if not isinstance(nums, list): | ||
raise TypeError("Input must be a list of numbers") | ||
|
||
# Filter out non-positive numbers and fractional parts | ||
nums = [int(x) for x in nums if x > 0 and x == int(x)] | ||
|
||
n = len(nums) | ||
|
||
# Move each number to its correct position | ||
for i in range(n): | ||
while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]: | ||
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] | ||
|
||
# Find the first missing positive integer | ||
for i in range(n): | ||
if nums[i] != i + 1: | ||
return i + 1 | ||
|
||
# If all integers from 1 to n are present, return n + 1 | ||
return n + 1 | ||
|
||
|
||
# Run the doctests | ||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Please add doctests for
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.
thanks for the feedback .. i have done it