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)
相关推荐
小刘同学++30 分钟前
用 OpenSSL 库实现 3DES(三重DES)加密
c++·算法·ssl
写写闲篇儿2 小时前
搜索二维矩阵
线性代数·算法·矩阵
LunaGeeking2 小时前
重要的城市(图论 最短路)
c++·算法·编程·图论·最短路·floyd
刘小小_算法工程师2 小时前
「ECG信号处理——(17)基于小波熵阈值的R峰检测(与时域-频域-多尺度小波法对比)」2025年6月12日
算法·信号处理
电控极客2 小时前
电动汽车驱动模式扭矩控制设计方法
经验分享·算法·汽车·策略模式
jz_ddk2 小时前
[python] 使用python设计滤波器
开发语言·python·学习·算法
1白天的黑夜13 小时前
二叉树-226.翻转链表-力扣(LeetCode)
数据结构·c++·leetcode
快乐肚皮3 小时前
快速排序优化技巧详解:提升性能的关键策略
java·算法·性能优化·排序算法
网安INF3 小时前
SHA-1算法详解:原理、特点与应用
java·算法·密码学
编程绿豆侠3 小时前
力扣HOT100之贪心算法:45. 跳跃游戏 II
leetcode·游戏·贪心算法