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\_pairs[i][0] > sorted\_pairs[j][1] f(i)=max{f(j)+1},ifsorted_pairs[i][0]>sorted_pairs[j][1]

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)
相关推荐
Raink老师1 天前
【AI面试临阵磨枪-62】设计基于 RAG 的内部知识库问答平台(多租户、权限、文件上传、实时更新)
人工智能·面试·职场和发展
smj2302_796826521 天前
解决leetcode第3943题递增后的数对数量
数据结构·python·算法·leetcode
炽烈小老头1 天前
【每天学习一点算法 2026/05/25】矩阵中的最长递增路径
学习·算法·矩阵
我爱cope1 天前
【Agent智能体6 | 智能体AI评估】
人工智能·职场和发展
叁散1 天前
实验报告:5G 仿真环境与基本链路模拟
算法
从负无穷开始的三次元代码生活1 天前
算法零碎灵感点分享
算法
染指11101 天前
9.LangChain框架(实现RAG)
数据库·人工智能·算法·机器学习·ai·大模型
我爱cope1 天前
【Agent智能体5 | 任务分解:识别工作流中的步骤】
人工智能·职场和发展
大数据三康1 天前
在spyder进行的遗传算法练习
开发语言·python·算法
Gene_20221 天前
轮式底盘的微分平坦
算法