Skip to content

Commit 48aaef0

Browse files
41. First Missing Positive (#102)
2 parents 77274c2 + 2c02f96 commit 48aaef0

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
41. First Missing Positive
3+
https://leetcode.com/problems/first-missing-positive/
4+
Problem:
5+
Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.
6+
You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.
7+
Example 1:
8+
Input: nums = [1,2,0]
9+
Output: 3
10+
Explanation: The numbers in the range [1,2] are all in the array.
11+
Example 2:
12+
Input: nums = [3,4,-1,1]
13+
Output: 2
14+
Explanation: 1 is in the array but 2 is missing.
15+
Example 3:
16+
Input: nums = [7,8,9,11,12]
17+
Output: 1
18+
Explanation: The smallest positive integer 1 is missing.
19+
Constraints:
20+
1 <= nums.length <= 10^5
21+
-2^31 <= nums[i] <= 2^31 - 1
22+
Explanation
23+
Initialize n
24+
const n = nums.length;
25+
This line sets the variable n to the length of the input array nums. It represents the size of the array.
26+
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.
27+
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.
28+
Return Next Positive Integer if All Elements Are in Place,
29+
If all elements are in their correct positions, the function returns the next positive integer after the maximum element in the array (n + 1).
30+
*/
31+
32+
33+
/**
34+
* @param {number[]} nums
35+
* @return {number}
36+
*/
37+
var firstMissingPositive = function(nums) {
38+
const n = nums.length;
39+
40+
for (let i = 0; i < n; i++)
41+
while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] !== nums[i])
42+
[nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]];
43+
44+
for (let i = 0; i < n; i++)
45+
if (nums[i] !== i + 1)
46+
return i + 1;
47+
48+
return n + 1;
49+
};
50+
51+
module.exports.firstMissingPositive = firstMissingPositive;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const assert = require("assert");
2+
const bitReverseToMakeNumberEqual = require("../../../LeetcodeProblems/Algorithms/hard/First_Missing_Positive").firstMissingPositive;
3+
4+
function test() {
5+
assert.deepEqual(
6+
firstMissingPositive([1,2,0]),
7+
3
8+
);
9+
assert.deepEqual(
10+
firstMissingPositive([3,4,-1,1]),
11+
2
12+
);
13+
assert.deepEqual(
14+
firstMissingPositive([7,8,9,11,12]),
15+
1
16+
);
17+
}
18+
19+
module.exports.test = test;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil
1616
| Name | Level | Link |
1717
|----------------------------------------------------------------------------------------------------------------------------------------------------------------|--------|---------------------------------------------------------------------------------------------------------------------|
1818
| [Edit Distance ](/LeetcodeProblems/Algorithms/hard/Edit_Distance.js) | Hard | https://leetcode.com/problems/edit-distance/ |
19+
| [First Missing Positive ](/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js) | Hard | https://leetcode.com/problems/first-missing-positive/ |
1920
| [Remove Invalid Parentheses ](/LeetcodeProblems/Algorithms/hard/Remove_Invalid_Parentheses.js) | Hard | https://leetcode.com/problems/remove-invalid-parentheses/ |
2021
| [Longest Consecutive Sequence ](/LeetcodeProblems/Algorithms/hard/Longest_Consecutive_Sequence.js) | Hard | https://leetcode.com/problems/longest-consecutive-sequence/ |
2122
| [Minimum Window Substring ](/LeetcodeProblems/Algorithms/hard/Minimum_Window_Substring.js) | Hard | https://leetcode.com/problems/minimum-window-substring/ |

0 commit comments

Comments
 (0)