Skip to content

Commit

Permalink
41
Browse files Browse the repository at this point in the history
  • Loading branch information
ACEMerlin committed May 12, 2019
1 parent aba1415 commit e9c9712
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions 41.first-missing-positive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
#
# @lc app=leetcode id=41 lang=ruby
#
# [41] First Missing Positive
#
# https://leetcode.com/problems/first-missing-positive/description/
#
# Given an unsorted integer array, find the smallest missing positive integer.
#
# Example 1:
#
# Input: [1,2,0]
# Output: 3
#
# Example 2:
#
# Input: [3,4,-1,1]
# Output: 2
#
# Example 3:
#
# Input: [7,8,9,11,12]
# Output: 1
#
# Note:
#
# Your algorithm should run in O(n) time and uses constant extra space.


# O(n) space.

# @param {Integer[]} nums
# @return {Integer}
def first_missing_positive(nums)
s = nums.size
xs = Array.new(s+1)
nums.each do |n|
xs[n] = n if n > 0 && n <= s
end
(1..s).each do |i|
return i if xs[i].nil?
end
xs.size
end


# O(1) space.

def first_missing_positive(nums)
s = nums.size
(0..s-1).each do |i|
while nums[i] > 0 && nums[i] <= s && nums[nums[i]-1] != nums[i]
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
end
end
(0..s-1).each do |i|
return i+1 if nums[i] != i+1
end
s+1
end

0 comments on commit e9c9712

Please sign in to comment.