Skip to content

Commit 93b389a

Browse files
authored
2025-01-06 v. 7.7.2: added "41. First Missing Positive"
2 parents d4aeba8 + a8c5727 commit 93b389a

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
641641
| 25. Reverse Nodes in k-Group | [Link](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Link](./lib/hard/25_reverse_nodes_in_k_group.rb) | [Link](./test/hard/test_25_reverse_nodes_in_k_group.rb) |
642642
| 30. Substring with Concatenation of All Words | [Link](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [Link](./lib/hard/30_substring_with_concatenation_of_all_words.rb) | [Link](./test/hard/test_30_substring_with_concatenation_of_all_words.rb) |
643643
| 32. Longest Valid Parentheses | [Link](https://leetcode.com/problems/longest-valid-parentheses/) | [Link](./lib/hard/32_longest_valid_parentheses.rb) | [Link](./test/hard/test_32_longest_valid_parentheses.rb) |
644+
| 41. First Missing Positive | [Link](https://leetcode.com/problems/first-missing-positive/) | [Link](./lib/hard/41_first_missing_positive.rb) | [Link](./test/hard/test_41_first_missing_positive.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.7.1'
8+
s.version = '7.7.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/first-missing-positive/
4+
# @param {Integer[]} nums
5+
# @return {Integer}
6+
def first_missing_positive(nums)
7+
result = 1
8+
nums.sort.each { |num| result += 1 if result == num }
9+
10+
result
11+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/41_first_missing_positive'
5+
require 'minitest/autorun'
6+
7+
class FirstMissingPositiveTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
3,
11+
first_missing_positive(
12+
[1, 2, 0]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
2,
20+
first_missing_positive(
21+
[3, 4, -1, 1]
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
1,
29+
first_missing_positive(
30+
[7, 8, 9, 11, 12]
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)