
g0001_0100.s0041_first_missing_positive.solution.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Udemy_Arrays
// #Big_O_Time_O(n)_Space_O(n) #2024_12_08_Time_1_ms_(98.86%)_Space_56.6_MB_(89.65%)
/**
* @param {number[]} nums
* @return {number}
*/
var firstMissingPositive = function(nums) {
for (let i = 0; i < nums.length; i++) {
while (
nums[i] > 0 &&
nums[i] <= nums.length &&
nums[nums[i] - 1] !== nums[i]
) {
// Swap nums[i] with nums[nums[i] - 1]
let temp = nums[nums[i] - 1]
nums[nums[i] - 1] = nums[i]
nums[i] = temp
}
}
for (let i = 0; i < nums.length; i++) {
if (nums[i] !== i + 1) {
return i + 1
}
}
return nums.length + 1
};
export { firstMissingPositive }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy