
g0001_0100.s0041_first_missing_positive.solution.ts 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) #2023_09_29_Time_55_ms_(98.51%)_Space_49.8_MB_(60.20%)
function firstMissingPositive(nums: number[]): number {
let i = 0
while (i < nums.length) {
let correctIndex = nums[i] - 1
if (nums[i] > 0 && nums[i] <= nums.length && nums[i] !== nums[correctIndex]) {
;[nums[i], nums[correctIndex]] = [nums[correctIndex], nums[i]]
} else {
i++
}
}
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