Skip to content

Commit e4c0fbb

Browse files
authored
Merge pull request #793 from fartem/516-Longest-Palindromic-Subsequence
2024-12-05 v. 7.2.2: added "516. Longest Palindromic Subsequence"
2 parents ac44b52 + c57cf83 commit e4c0fbb

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
587587
| 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) |
588588
| 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) |
589589
| 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) |
590+
| 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) |

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.2.1'
8+
s.version = '7.2.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/longest-palindromic-subsequence/
4+
# @param {String} s
5+
# @return {Integer}
6+
def longest_palindrome_subseq(s) = calculate_length(s, 0, s.size - 1, {})
7+
8+
private
9+
10+
# @param {String} s
11+
# @param {Integer} st
12+
# @param {Integer} nd
13+
# @param {Map<Integer, Integer>} subs
14+
# @return {Integer}
15+
def calculate_length(s, st, nd, subs)
16+
return 0 if st > nd
17+
18+
return 1 if st == nd
19+
20+
s_ch = s[st]
21+
e_ch = s[nd]
22+
key = "#{st}/#{nd}"
23+
unless subs.include?(key)
24+
curr_length =
25+
if s_ch == e_ch
26+
calculate_length(s, st + 1, nd - 1, subs) + 2
27+
else
28+
[
29+
calculate_length(s, st + 1, nd, subs),
30+
calculate_length(s, st, nd - 1, subs)
31+
].max
32+
end
33+
34+
subs[key] = curr_length
35+
end
36+
37+
subs[key]
38+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/516_longest_palindromic_subsequence'
5+
require 'minitest/autorun'
6+
7+
class LongestPalindromicSubsequenceTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
4,
11+
longest_palindrome_subseq(
12+
'bbbab'
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
2,
20+
longest_palindrome_subseq(
21+
'cbbd'
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)