Skip to content

Commit

Permalink
[Array] Add a solution to First Missing Positive
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jul 7, 2018
1 parent 1980d89 commit ae4c7d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 22 additions & 0 deletions Array/FirstMissingPositive.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Question Link: https://leetcode.com/problems/first-missing-positive/
* Primary idea: Use a set to hold number in the array and iterate through 1...nums.count to find the missing one
* Time Complexity: O(n), Space Complexity: O(n)
*
*/

class FirstMissingPositive {
func firstMissingPositive(_ nums: [Int]) -> Int {
var set = Set<Int>()

nums.forEach { set.insert($0) }

for i in 0..<nums.count {
if !set.contains(i + 1) {
return i + 1
}
}

return nums.count + 1
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 269 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 270 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).


## Array
Expand All @@ -40,6 +40,7 @@
[Island Perimeter](https://leetcode.com/problems/island-perimeter/)| [Swift](./Array/IslandPerimeter.swift)| Easy| O(nm)| O(1)|
[Majority Element](https://leetcode.com/problems/majority-element/)| [Swift](./Array/MajorityElement.swift)| Easy| O(n)| O(1)|
[Majority Element II](https://leetcode.com/problems/majority-element-ii/)| [Swift](./Array/MajorityElementII.swift)| Medium| O(n)| O(1)|
[First Missing Positive](https://leetcode.com/problems/first-missing-positive/)| [Swift](./Array/FirstMissingPositive.swift)| Hard| O(n)| O(n)|
[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)| [Swift](./Array/IntersectionTwoArrays.swift)| Easy| O(n)| O(n)|
[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| [Swift](./Array/IntersectionTwoArraysII.swift)| Easy| O(n)| O(n)|
[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)| [Swift](./Array/ContainsDuplicate.swift)| Easy| O(n)| O(n)|
Expand Down Expand Up @@ -787,7 +788,7 @@
| [Swift](./DP/WildcardMatching.swift) | 44 | [Wildcard Matching](https://oj.leetcode.com/problems/wildcard-matching/) | Hard |
| [Swift](./String/MultiplyStrings.swift) | 43 | [Multiply Strings](https://oj.leetcode.com/problems/multiply-strings/) | Medium |
| [Swift](./Math/TrappingRainWater.swift) | 42 | [Trapping Rain Water](https://oj.leetcode.com/problems/trapping-rain-water/) | Hard |
| | 41 | [First Missing Positive](https://oj.leetcode.com/problems/first-missing-positive/) | Hard |
| [Swift](./Array/FirstMissingPositive.swift) | 41 | [First Missing Positive](https://oj.leetcode.com/problems/first-missing-positive/) | Hard |
| [Swift](./DFS/combinationSumII.swiftc) | 40 | [Combination Sum II](https://oj.leetcode.com/problems/combination-sum-ii/) | Medium |
| [Swift](./DFS/CombinationSum.swift) | 39 | [Combination Sum](https://oj.leetcode.com/problems/combination-sum/) | Medium |
| [Swift](./String/CountAndSay.swift) | 38 | [Count and Say](https://oj.leetcode.com/problems/count-and-say/) | Easy |
Expand Down

0 comments on commit ae4c7d6

Please sign in to comment.