|
| 1 | +--- |
| 2 | +id: 41FirstMissingPositive |
| 3 | +title: First Missing Positive (LeetCode) |
| 4 | +sidebar_label: 0041-First Missing Positive |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Hash Table |
| 8 | +description: Find the smallest missing positive integer. |
| 9 | +sidebar_position: 41 |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 15 | +| :---------------- | :------------ | :--------------- | |
| 16 | +| [First Missing Positive](https://leetcode.com/problems/first-missing-positive/description/) | [First Missing Positive Solution on LeetCode](https://leetcode.com/problems/first-missing-positive/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | |
| 17 | + |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +Given an unsorted integer array `nums`, return the smallest missing positive integer. |
| 22 | + |
| 23 | +You must implement an algorithm that runs in `O(n)` time and uses constant extra space. |
| 24 | + |
| 25 | +### Example 1 |
| 26 | + |
| 27 | +- **Input:** `nums = [1,2,0]` |
| 28 | +- **Output:** `3` |
| 29 | +- **Explanation:** The smallest missing positive integer is `3`. |
| 30 | + |
| 31 | +### Example 2 |
| 32 | + |
| 33 | +- **Input:** `nums = [3,4,-1,1]` |
| 34 | +- **Output:** `2` |
| 35 | +- **Explanation:** The smallest missing positive integer is `2`. |
| 36 | + |
| 37 | +### Example 3 |
| 38 | + |
| 39 | +- **Input:** `nums = [7,8,9,11,12]` |
| 40 | +- **Output:** `1` |
| 41 | +- **Explanation:** The smallest missing positive integer is `1`. |
| 42 | + |
| 43 | +### Constraints |
| 44 | + |
| 45 | +- `1 <= nums.length <= 10^5` |
| 46 | +- `-2^31 <= nums[i] <= 2^31 - 1` |
| 47 | + |
| 48 | +## Approach |
| 49 | + |
| 50 | +To solve the problem, we can use the following approach: |
| 51 | + |
| 52 | +1. **Mark Elements Out of Range**: |
| 53 | + - Iterate through the array and mark elements that are out of the range `[1, n]` by setting them to a number greater than `n`. |
| 54 | + |
| 55 | +2. **Use Indices as Markers**: |
| 56 | + - Use the indices of the array to mark the presence of numbers by negating the value at the corresponding index. |
| 57 | + |
| 58 | +3. **Identify the Missing Positive**: |
| 59 | + - Iterate through the array again to find the first positive value, which indicates the missing positive integer. |
| 60 | + |
| 61 | +### Solution Code |
| 62 | + |
| 63 | +#### Python |
| 64 | + |
| 65 | +```python |
| 66 | +class Solution: |
| 67 | + def firstMissingPositive(self, nums: List[int]) -> int: |
| 68 | + n = len(nums) |
| 69 | + |
| 70 | + for i in range(n): |
| 71 | + if nums[i] <= 0 or nums[i] > n: |
| 72 | + nums[i] = n + 1 |
| 73 | + |
| 74 | + for i in range(n): |
| 75 | + num = abs(nums[i]) |
| 76 | + if num <= n: |
| 77 | + nums[num - 1] = -abs(nums[num - 1]) |
| 78 | + |
| 79 | + for i in range(n): |
| 80 | + if nums[i] > 0: |
| 81 | + return i + 1 |
| 82 | + |
| 83 | + return n + 1 |
| 84 | +``` |
| 85 | + |
| 86 | +#### Java |
| 87 | + |
| 88 | +```java |
| 89 | +class Solution { |
| 90 | + public int firstMissingPositive(int[] nums) { |
| 91 | + int n = nums.length; |
| 92 | + |
| 93 | + for (int i = 0; i < n; i++) { |
| 94 | + if (nums[i] <= 0 || nums[i] > n) { |
| 95 | + nums[i] = n + 1; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for (int i = 0; i < n; i++) { |
| 100 | + int num = Math.abs(nums[i]); |
| 101 | + if (num <= n) { |
| 102 | + nums[num - 1] = -Math.abs(nums[num - 1]); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for (int i = 0; i < n; i++) { |
| 107 | + if (nums[i] > 0) { |
| 108 | + return i + 1; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return n + 1; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +#### C++ |
| 118 | + |
| 119 | +```c++ |
| 120 | +#include <vector> |
| 121 | +#include <cmath> |
| 122 | + |
| 123 | +using namespace std; |
| 124 | + |
| 125 | +class Solution { |
| 126 | +public: |
| 127 | + int firstMissingPositive(vector<int>& nums) { |
| 128 | + int n = nums.size(); |
| 129 | + |
| 130 | + for (int i = 0; i < n; i++) { |
| 131 | + if (nums[i] <= 0 || nums[i] > n) { |
| 132 | + nums[i] = n + 1; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + for (int i = 0; i < n; i++) { |
| 137 | + int num = abs(nums[i]); |
| 138 | + if (num <= n) { |
| 139 | + nums[num - 1] = -abs(nums[num - 1]); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + for (int i = 0; i < n; i++) { |
| 144 | + if (nums[i] > 0) { |
| 145 | + return i + 1; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return n + 1; |
| 150 | + } |
| 151 | +}; |
| 152 | +``` |
| 153 | + |
| 154 | +### Conclusion: |
| 155 | +This approach ensures that the smallest missing positive integer is found efficiently, using constant extra space. |
0 commit comments