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)
相关推荐
ychqsq22 分钟前
88.归途
经验分享·职场和发展
巧克力男孩dd1 小时前
Python超典型练习题(第一次作业)
开发语言·python·算法
爱刷碗的苏泓舒1 小时前
平方根信息滤波:矩阵推导及 GNSS 参数估计应用
线性代数·算法·矩阵·gnss·参数估计·测量平差·平方根信息滤波
想做小南娘,发现自己是女生喵2 小时前
第 2 章 顺序表和 vector
java·数据结构·算法
艾醒3 小时前
2026年第29周(7.13-7.19)AI全复盘:技术突破、行业趣闻翻车、算力服务器商业动态
人工智能·算法
雪碧聊技术3 小时前
动态规划算法—01背包问题
算法·动态规划
bu_shuo3 小时前
c与cpp中的argc和argv
c语言·c++·算法
普贤莲花3 小时前
【2026年第29周---写于20260718】---整理,断舍离
程序人生·算法·生活
Reart4 小时前
Leetcode 674.最长连续递增序列 (719)
后端·算法
Reart4 小时前
Leetcode 300.最长递增子序列(719)
算法