Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(positive): first missing positive cpp #498

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions C++/first-missing-positive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Problem -
41. First Missing Positive
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.

The Optimized Solution -
Time Complexity: O(N)
Space Complexity: O(1)

Example -
Input: nums = [1,2,0]
Output: 3
Explanation: The numbers in the range [1,2] are all in the array.

Input: nums = [3,4,-1,1]
Output: 2
Explanation: 1 is in the array but 2 is missing.
*/


class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();

for (int i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums[i], nums[nums[i] - 1]);
}
}

for (int i = 0; i < n; i++) {
if (i + 1 != nums[i]) {
return i + 1;
}
}

return n + 1;
}
};
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/) | [Java](./Java/can-make-arithmetic-progression-from-sequence.java) | _O(n)_ | _O(1)_ | Easy | Array | |
| 122 | [Best Time to buy and sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [Python](./Python/best-time-to-buy-and-sell-stock-ii.py) <br> [C++](./C++/Best-Time-to-Buy-and-Sell-Stock-II.cpp) | _O(N)_ | _O(1)_ | Medium | Stocks | |
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [Python](./Python/pascals-triangle-ii.py) | _O(N^2)_ | _O(K)_ | Easy | | |
| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Java](./Java/running-sum-of-1d-array.java) | _O(N)_ | _O(N)_ | Easy | Simple sum | |
| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Java](./Java/running-sum-of-1d-array.java) | _O(N)_ | _O(N)_ | Easy | Simple sum |
| 41 | [First Missing Positive](https://leetcode.com/problems/first-missing-positive/) | [C++](./C++/first-missing-positive.cpp) | _O(N)_ | _O(1)_ | Hard | Array | |
| 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./Python/trapping_rain.py) | _O(N^2)_ | _O(N)_ | Hard | Array | |
| 11 | [Container with Most Water](https://leetcode.com/problems/container-with-most-water/) | [Python](./Python/container_with_most_water.py) | _O(N)_ | _O(N)_ | Medium | Array Two Pointers | |
| 1134 🔒 | [Armstrong Number](https://leetcode.com/problems/armstrong-number/) | [Java](./Java/Armstrong-Number.java) | _O(N)_ | _O(1)_ | Easy | | |
Expand Down