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)
相关推荐
potato_may4 分钟前
C++ 发展简史与核心语法入门
开发语言·c++·算法
Liangwei Lin6 分钟前
洛谷 P1443 马的遍历
数据结构·算法
老鱼说AI8 分钟前
算法基础教学第二步:数组(超级详细原理级别讲解)
数据结构·神经网络·算法·链表
小白程序员成长日记30 分钟前
2025.12.01 力扣每日一题
算法·leetcode·职场和发展
爱装代码的小瓶子30 分钟前
【cpp知识铺子】map和set的前身-二叉搜索树
c++·算法
TL滕1 小时前
从0开始学算法——第四天(练点题吧)
数据结构·笔记·学习·算法
[J] 一坚1 小时前
华为OD、微软、Google、神州数码、腾讯、中兴、网易有道C/C++字符串、数组、链表、树等笔试真题精粹
c语言·数据结构·c++·算法·链表
多则惑少则明1 小时前
【算法题4】找出字符串中的最长回文子串(Java版)
java·开发语言·数据结构·算法
迷途之人不知返1 小时前
二叉树题目
数据结构·算法
优宁维生物2 小时前
DNA 提取的基础方法
人工智能·算法