Leetcode 646. Maximum Length of Pair Chain

Problem

Given a string s, find the longest palindromic subsequence's length in s.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Algorithm

Dynamic Programming (DP). Sort the data with a and b, define the state f(i) is the longest palindromic subsequence's length, then
f ( i ) = m a x { f ( j ) + 1 } , i f s o r t e d _ p a i r s i 0 > s o r t e d _ p a i r s j 1 f(i) = max\{ f(j) + 1 \}, \quad if \quad sorted\_pairsi0 > sorted\_pairsj1 f(i)=max{f(j)+1},ifsorted_pairsi0>sorted_pairsj1

Code

python3 复制代码
class Solution:
    def findLongestChain(self, pairs: List[List[int]]) -> int:
        plen = len(pairs)
        dp = [1] * plen
        sorted_pairs = sorted(pairs, key=lambda x: (x[0], x[1]))
        for i in range(1, plen):
            for j in range(i):
                if sorted_pairs[i][0] > sorted_pairs[j][1] and dp[i] <= dp[j]:
                     dp[i] = dp[j] + 1
        
        return max(dp)
相关推荐
旖-旎10 分钟前
《LeetCode 130 被围绕的区域 FloodFill DFS 解法》
c++·算法·深度优先·力扣·floodfill
林森lsjs11 分钟前
斐波那契数列的 N 种解法:从递归到动态规划的优化之路【算法思考】
算法·动态规划
apcipot_rain1 小时前
计科八股20260616(1)——堆存中位数、链表判环、黑白测试、敏捷开发与瀑布模型、配置管理、持续集成、池化
数据结构·算法·软件工程
JAVA面经实录9178 小时前
Java 数据结构与算法 (终极完整学习文档)
java·数据结构·算法
程序员三藏9 小时前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
开源Z9 小时前
LeetCode 42 · 接雨水:从暴力到双指针的三步优化
算法·leetcode
旖-旎9 小时前
《LeetCode 695 岛屿的最大面积 FloodFill DFS 解法》
c++·算法·力扣·深度优先遍历·floodfill
syagain_zsx10 小时前
STL 之 vector 讲练结合
c++·算法
MartinYeung511 小时前
[论文学习]DP2Unlearning:高效且具保证的大型语言模型遗忘框架(基于差分隐私的 LLM Unlearning 方法)
学习·算法·语言模型