- Notifications
You must be signed in to change notification settings - Fork98
41. First Missing Positive#102
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
ignacio-chiazzo merged 4 commits intoignacio-chiazzo:masterfromchhavibansal:missing_positiveOct 22, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletionsLeetcodeProblems/Algorithms/hard/First_Missing_Positive.js
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,51 @@ | ||
/* | ||
41. First Missing Positive | ||
https://leetcode.com/problems/first-missing-positive/ | ||
Problem: | ||
Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums. | ||
You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space. | ||
Example 1: | ||
Input: nums = [1,2,0] | ||
Output: 3 | ||
Explanation: The numbers in the range [1,2] are all in the array. | ||
Example 2: | ||
Input: nums = [3,4,-1,1] | ||
Output: 2 | ||
Explanation: 1 is in the array but 2 is missing. | ||
Example 3: | ||
Input: nums = [7,8,9,11,12] | ||
Output: 1 | ||
Explanation: The smallest positive integer 1 is missing. | ||
Constraints: | ||
1 <= nums.length <= 10^5 | ||
-2^31 <= nums[i] <= 2^31 - 1 | ||
Explanation | ||
Initialize n | ||
const n = nums.length; | ||
This line sets the variable n to the length of the input array nums. It represents the size of the array. | ||
This is the cyclic sort algorithm. It iterates through the array and, in each step, it checks if the current element nums[i] is within the valid range (1 to n) and not in its correct position. If so, it swaps the element with the one at its correct position. | ||
After the cyclic sort, this loop searches for the first element that is out of place. If nums[i] is not equal to i + 1, it means that i + 1 is the smallest missing positive integer, and it is returned. | ||
Return Next Positive Integer if All Elements Are in Place, | ||
If all elements are in their correct positions, the function returns the next positive integer after the maximum element in the array (n + 1). | ||
*/ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var firstMissingPositive = function(nums) { | ||
const n = nums.length; | ||
for (let i = 0; i < n; i++) | ||
while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] !== nums[i]) | ||
[nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]]; | ||
for (let i = 0; i < n; i++) | ||
if (nums[i] !== i + 1) | ||
return i + 1; | ||
return n + 1; | ||
}; | ||
module.exports.firstMissingPositive = firstMissingPositive; |
19 changes: 19 additions & 0 deletionsLeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js
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,19 @@ | ||
const assert = require("assert"); | ||
const bitReverseToMakeNumberEqual = require("../../../LeetcodeProblems/Algorithms/hard/First_Missing_Positive").firstMissingPositive; | ||
function test() { | ||
assert.deepEqual( | ||
firstMissingPositive([1,2,0]), | ||
3 | ||
); | ||
assert.deepEqual( | ||
firstMissingPositive([3,4,-1,1]), | ||
2 | ||
); | ||
assert.deepEqual( | ||
firstMissingPositive([7,8,9,11,12]), | ||
1 | ||
); | ||
} | ||
module.exports.test = test; |
1 change: 1 addition & 0 deletionsREADME.md
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.