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)
相关推荐
洛水水2 分钟前
【力扣100题】31.二叉树的层序遍历
算法·leetcode·职场和发展
君义_noip8 分钟前
CSP-S 2025 入门级 第一轮(初赛) 完善程序(1)
c++·算法·信息学奥赛·初赛·csp 第一轮
洛水水11 分钟前
【力扣100题】41.爬楼梯
算法·leetcode·职场和发展
Pkmer38 分钟前
LeetCode 上极少见的工程级滑窗实现
python·leetcode
sheeta19982 小时前
LeetCode 每日一题笔记 日期:2026.05.13 题目:1674. 使数组互补的最少操作次数
笔记·算法·leetcode
liulilittle2 小时前
TCP UCP v1.0:BBR 的非破坏性约束层
网络·c++·网络协议·tcp/ip·算法·c·通信
每天回答3个问题2 小时前
LeetCodeHot100|回溯算法、46.全排列、78.子集、17.电话号码的字母组合
算法·深度优先·回溯
YL200404262 小时前
038翻转二叉树
数据结构·leetcode
Liangwei Lin3 小时前
LeetCode 287. 寻找重复数
算法·leetcode·职场和发展
Daorigin_com3 小时前
道本科技三大系统形成的“合同—合规—法务”智能闭环!
科技·职场和发展·分类·服务发现·边缘计算·集成学习·敏捷流程