Skip to content

Commit

Permalink
2024-12-05 v. 7.2.2: added "516. Longest Palindromic Subsequence"
Browse files Browse the repository at this point in the history
  • Loading branch information
fartem committed Dec 5, 2024
1 parent ac44b52 commit c57cf83
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 508. Most Frequent Subtree Sum | [Link](https://leetcode.com/problems/most-frequent-subtree-sum/) | [Link](./lib/medium/508_most_frequent_subtree_sum.rb) | [Link](./test/medium/test_508_most_frequent_subtree_sum.rb) |
| 513. Find Bottom Left Tree Value | [Link](https://leetcode.com/problems/find-bottom-left-tree-value/) | [Link](./lib/medium/513_find_bottom_left_tree_value.rb) | [Link](./test/medium/test_513_find_bottom_left_tree_value.rb) |
| 515. Find Largest Value in Each Tree Row | [Link](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | [Link](./lib/medium/515_find_largest_value_in_each_tree_row.rb) | [Link](./test/medium/test_515_find_largest_value_in_each_tree_row.rb) |
| 516. Longest Palindromic Subsequence | [Link](https://leetcode.com/problems/longest-palindromic-subsequence/) | [Link](./lib/medium/516_longest_palindromic_subsequence.rb) | [Link](./test/medium/test_516_longest_palindromic_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 = '7.2.1'
s.version = '7.2.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
38 changes: 38 additions & 0 deletions lib/medium/516_longest_palindromic_subsequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# https://leetcode.com/problems/longest-palindromic-subsequence/
# @param {String} s
# @return {Integer}
def longest_palindrome_subseq(s) = calculate_length(s, 0, s.size - 1, {})

private

# @param {String} s
# @param {Integer} st
# @param {Integer} nd
# @param {Map<Integer, Integer>} subs
# @return {Integer}
def calculate_length(s, st, nd, subs)
return 0 if st > nd

return 1 if st == nd

s_ch = s[st]
e_ch = s[nd]
key = "#{st}/#{nd}"
unless subs.include?(key)
curr_length =
if s_ch == e_ch
calculate_length(s, st + 1, nd - 1, subs) + 2
else
[
calculate_length(s, st + 1, nd, subs),
calculate_length(s, st, nd - 1, subs)
].max
end

subs[key] = curr_length
end

subs[key]
end
25 changes: 25 additions & 0 deletions test/medium/test_516_longest_palindromic_subsequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/516_longest_palindromic_subsequence'
require 'minitest/autorun'

class LongestPalindromicSubsequenceTest < ::Minitest::Test
def test_default_one
assert_equal(
4,
longest_palindrome_subseq(
'bbbab'
)
)
end

def test_default_two
assert_equal(
2,
longest_palindrome_subseq(
'cbbd'
)
)
end
end

0 comments on commit c57cf83

Please sign in to comment.