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)
相关推荐
深圳市益普科技有限公司4 分钟前
半导体良率管理YMS:良率与设备参数的相关性,先拉哪几组数据
人工智能·python·算法
程序员清风1 小时前
算法题中的边界条件、测试与调试
开发语言·算法·kotlin
炘爚2 小时前
数据结构算法总结
数据结构·python·算法
Dageno_AI2 小时前
ChatGPT 不再引用 Reddit 之后,还值得在 Reddit 上做 AI 可见度吗?
算法
hetao17338372 小时前
2026-10-25 hetao1733837 的刷题记录
c++·算法
Jsundoku2 小时前
第七章 机器学习 -- 其他经典算法
人工智能·算法·机器学习
进击的丸子3 小时前
基于 ArcFace SDK 的跨平台人脸特征值下发、同步以及存储方案
后端·算法·代码规范
见叶之秋3 小时前
【C++】红与黑的边界:红黑树从插入到自检
数据结构·c++·算法·面试
考研保研资料分享3 小时前
2023历史学跨保山大马理论:两轮笔面试、课程论文与方向取舍
面试·职场和发展
一条闲鱼_mytube4 小时前
分布式 ID 生成方案:雪花算法与号段模式
分布式·算法·wpf