Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 041273a

Browse files
author
mukul
committed
[41] First missing positive
1 parent 77274c2 commit 041273a

File tree

10 files changed

+214
-83
lines changed

10 files changed

+214
-83
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Algorithms-Leetcode-Javascript.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/Project.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
41. First Missing Positive
3+
https://leetcode.com/problems/first-missing-positive/
4+
5+
Problem:
6+
Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.
7+
8+
You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.
9+
10+
11+
Example 1:
12+
13+
Input: nums = [1,2,0]
14+
Output: 3
15+
Explanation: The numbers in the range [1,2] are all in the array.
16+
Example 2:
17+
18+
Input: nums = [3,4,-1,1]
19+
Output: 2
20+
Explanation: 1 is in the array but 2 is missing.
21+
Example 3:
22+
23+
Input: nums = [7,8,9,11,12]
24+
Output: 1
25+
Explanation: The smallest positive integer 1 is missing.
26+
27+
28+
Constraints:
29+
30+
1 <= nums.length <= 10^5
31+
-2^31 <= nums[i] <= 2^31 - 1
32+
33+
Explanation
34+
35+
Initialize n
36+
const n = nums.length;
37+
This line sets the variable n to the length of the input array nums. It represents the size of the array.
38+
39+
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.
40+
41+
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.
42+
43+
44+
Return Next Positive Integer if All Elements Are in Place,
45+
If all elements are in their correct positions, the function returns the next positive integer after the maximum element in the array (n + 1).
46+
*/
47+
48+
49+
/**
50+
* @param {number[]} nums
51+
* @return {number}
52+
*/
53+
var firstMissingPositive = function(nums) {
54+
const n = nums.length
55+
56+
for (let i = 0; i < n; i++)
57+
while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] !== nums[i])
58+
[nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]]
59+
60+
for (let i = 0; i < n; i++)
61+
if (nums[i] !== i + 1)
62+
return i + 1
63+
64+
return n + 1
65+
};
66+
67+
module.exports.firstMissingPositive = firstMissingPositive;
Lines changed: 19 additions & 0 deletions
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+
2
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

Lines changed: 84 additions & 83 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)