Skip to content

Commit 3e8f436

Browse files
authored
Merge pull request #749 from fartem/300_Longest_Increasing_Subsequence
2024-10-16 v. 6.8.2: added "300. Longest Increasing Subsequence"
2 parents 17a2781 + 2088a76 commit 3e8f436

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
547547
| 284. Peeking Iterator | [Link](https://leetcode.com/problems/peeking-iterator/) | [Link](./lib/medium/284_peeking_iterator.rb) | [Link](./test/medium/test_284_peeking_iterator.rb) |
548548
| 287. Find the Duplicate Number | [Link](https://leetcode.com/problems/find-the-duplicate-number/) | [Link](./lib/medium/287_find_the_duplicate_number.rb) | [Link](./test/medium/test_287_find_the_duplicate_number.rb) |
549549
| 299. Bulls and Cows | [Link](https://leetcode.com/problems/bulls-and-cows/) | [Link](./lib/medium/299_bulls_and_cows.rb) | [Link](./test/medium/test_299_bulls_and_cows.rb) |
550+
| 300. Longest Increasing Subsequence | [Link](https://leetcode.com/problems/longest-increasing-subsequence/) | [Link](./lib/medium/300_longest_increasing_subsequence.rb) | [Link](./test/medium/test_300_longest_increasing_subsequence.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 = '6.8.1'
8+
s.version = '6.8.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/longest-increasing-subsequence/
4+
# @param {Integer[]} nums
5+
# @return {Integer}
6+
def length_of_lis(nums)
7+
result = []
8+
nums.each do |num|
9+
index = result.bsearch_index { |checked_num| checked_num >= num } || result.size
10+
result[index] = num
11+
end
12+
13+
result.size
14+
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/medium/300_longest_increasing_subsequence'
5+
require 'minitest/autorun'
6+
7+
class LongestIncreasingSubsequenceTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
4,
11+
length_of_lis(
12+
[10, 9, 2, 5, 3, 7, 101, 18]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
4,
20+
length_of_lis(
21+
[0, 1, 0, 3, 2, 3]
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
1,
29+
length_of_lis(
30+
[7, 7, 7, 7, 7, 7, 7]
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)