Skip to content

Commit

Permalink
Merge pull request #749 from fartem/300_Longest_Increasing_Subsequence
Browse files Browse the repository at this point in the history
2024-10-16 v. 6.8.2: added "300. Longest Increasing Subsequence"
  • Loading branch information
fartem authored Oct 16, 2024
2 parents 17a2781 + 2088a76 commit 3e8f436
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.8.1'
s.version = '6.8.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
14 changes: 14 additions & 0 deletions lib/medium/300_longest_increasing_subsequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# https://leetcode.com/problems/longest-increasing-subsequence/
# @param {Integer[]} nums
# @return {Integer}
def length_of_lis(nums)
result = []
nums.each do |num|
index = result.bsearch_index { |checked_num| checked_num >= num } || result.size
result[index] = num
end

result.size
end
34 changes: 34 additions & 0 deletions test/medium/test_300_longest_increasing_subsequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/300_longest_increasing_subsequence'
require 'minitest/autorun'

class LongestIncreasingSubsequenceTest < ::Minitest::Test
def test_default_one
assert_equal(
4,
length_of_lis(
[10, 9, 2, 5, 3, 7, 101, 18]
)
)
end

def test_default_two
assert_equal(
4,
length_of_lis(
[0, 1, 0, 3, 2, 3]
)
)
end

def test_default_three
assert_equal(
1,
length_of_lis(
[7, 7, 7, 7, 7, 7, 7]
)
)
end
end

0 comments on commit 3e8f436

Please sign in to comment.