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)
相关推荐
Q一件事34 分钟前
RWEQ——保留与消去P的soil_loss联合推导
算法
HugoStudio_SWAN1 小时前
洛谷 P10719 \[GESP202406 五级] 黑白格——暴力美学与图像处理的最小外接矩形
c++·图像处理·人工智能·学习·程序人生·算法·目标跟踪
那年窗外下的雪.1 小时前
AIDC 学习日志|第 20 天|多归属业务验收与哈希不均定位
学习·算法·哈希算法
s_w.h3 小时前
【 刷题 】双指针
算法
啥都想学点的研究生3 小时前
一篇文章讲清楚:K-Means聚类算法
算法·kmeans·聚类
白狐_7983 小时前
408 数据结构|KMP做题方法:next、nextval和高频易错点
数据结构·算法
月华路3 小时前
《模型不玄学》第20章 两种评估视角
人工智能·算法·机器学习
禹凕3 小时前
快速幂算法详解与实战
python·算法
xxwl5853 小时前
高斯消元法异或版
人工智能·算法·机器学习