From 693a1059f6195472619e1ea97f0f44773f305afd Mon Sep 17 00:00:00 2001 From: mukul Date: Tue, 22 Oct 2024 20:00:00 +0530 Subject: [PATCH 1/4] 41. First Missing Positive --- .../Algorithms/hard/First_Missing_Positive.js | 51 +++++++++++++++++++ .../hard/First_Missing_Positive_Test.js | 19 +++++++ README.md | 1 + 3 files changed, 71 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js create mode 100644 LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js diff --git a/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js b/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js new file mode 100644 index 0000000..6a39e8a --- /dev/null +++ b/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js @@ -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; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js b/LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js new file mode 100644 index 0000000..c9b77c7 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js @@ -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]), + 2 + ); + assert.deepEqual( + firstMissingPositive([3,4,-1,1]), + 2 + ); + assert.deepEqual( + firstMissingPositive([7,8,9,11,12]), + 1 +); +} + +module.exports.test = test; \ No newline at end of file diff --git a/README.md b/README.md index d820f21..01307b7 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil | Name | Level | Link | |----------------------------------------------------------------------------------------------------------------------------------------------------------------|--------|---------------------------------------------------------------------------------------------------------------------| | [Edit Distance ](/LeetcodeProblems/Algorithms/hard/Edit_Distance.js) | Hard | https://leetcode.com/problems/edit-distance/ | +| [First Missing Positive ](/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js) | Hard | https://leetcode.com/problems/first-missing-positive/ | | [Remove Invalid Parentheses ](/LeetcodeProblems/Algorithms/hard/Remove_Invalid_Parentheses.js) | Hard | https://leetcode.com/problems/remove-invalid-parentheses/ | | [Longest Consecutive Sequence ](/LeetcodeProblems/Algorithms/hard/Longest_Consecutive_Sequence.js) | Hard | https://leetcode.com/problems/longest-consecutive-sequence/ | | [Minimum Window Substring ](/LeetcodeProblems/Algorithms/hard/Minimum_Window_Substring.js) | Hard | https://leetcode.com/problems/minimum-window-substring/ | From 8a907b6f2f3be62f21e04cc0f2b4ee6c8849f7fe Mon Sep 17 00:00:00 2001 From: mukul Date: Tue, 22 Oct 2024 20:03:32 +0530 Subject: [PATCH 2/4] 41. First Missing Positive --- .../Algorithms/hard/First_Missing_Positive_Test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js b/LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js index c9b77c7..8662c2c 100644 --- a/LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js +++ b/LeetcodeProblemsTests/Algorithms/hard/First_Missing_Positive_Test.js @@ -4,7 +4,7 @@ const bitReverseToMakeNumberEqual = require("../../../LeetcodeProblems/Algorithm function test() { assert.deepEqual( firstMissingPositive([1,2,0]), - 2 + 3 ); assert.deepEqual( firstMissingPositive([3,4,-1,1]), From cd335dc02a8555a6469ee432f4d407a3c3b51cb7 Mon Sep 17 00:00:00 2001 From: mukul Date: Tue, 22 Oct 2024 20:04:31 +0530 Subject: [PATCH 3/4] 41. First Missing Positive --- .../Algorithms/hard/First_Missing_Positive.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js b/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js index 6a39e8a..c85597b 100644 --- a/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js +++ b/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js @@ -35,17 +35,17 @@ If all elements are in their correct positions, the function returns the next po * @return {number} */ var firstMissingPositive = function(nums) { - const n = nums.length + 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++) + 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 + for (let i = 0; i < n; i++) + if (nums[i] !== i + 1) + return i + 1 - return n + 1 + return n + 1 }; module.exports.firstMissingPositive = firstMissingPositive; \ No newline at end of file From 2c02f96c7183a278d1e0323e8da2a232fe50ed32 Mon Sep 17 00:00:00 2001 From: mukul Date: Tue, 22 Oct 2024 20:28:17 +0530 Subject: [PATCH 4/4] 41. First Missing Positive --- .../Algorithms/hard/First_Missing_Positive.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js b/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js index c85597b..8bb6ed3 100644 --- a/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js +++ b/LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js @@ -35,17 +35,17 @@ If all elements are in their correct positions, the function returns the next po * @return {number} */ var firstMissingPositive = function(nums) { - const n = nums.length + 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]] + [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 i + 1; - return n + 1 + return n + 1; }; module.exports.firstMissingPositive = firstMissingPositive; \ No newline at end of file