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)
相关推荐
前端小L40 分钟前
图论专题(十九):DAG上的“关键路径”——极限规划「并行课程 III」
算法·矩阵·深度优先·图论·宽度优先
scx2013100442 分钟前
20251116 树状DP总结
算法·深度优先·图论
Aspect of twilight1 小时前
LeetCode华为大模型岗刷题
python·leetcode·华为·力扣·算法题
2301_807997381 小时前
代码随想录-day47
数据结构·c++·算法·leetcode
Elias不吃糖1 小时前
LeetCode每日一练(3)
c++·算法·leetcode
小龙报1 小时前
《算法通关指南数据结构和算法篇(2)--- 链表专题》
c语言·数据结构·c++·算法·链表·学习方法·visual studio
艾莉丝努力练剑2 小时前
【优选算法必刷100题】第031~32题(前缀和算法):连续数组、矩阵区域和
大数据·人工智能·线性代数·算法·矩阵·二维前缀和
醉颜凉2 小时前
环形房屋如何 “安全劫舍”?动态规划解题逻辑与技巧
c语言·算法·动态规划
大雨淅淅2 小时前
一文搞懂动态规划:从入门到精通
算法·动态规划
不去幼儿园2 小时前
【启发式算法】灰狼优化算法(Grey Wolf Optimizer, GWO)详细介绍(Python)
人工智能·python·算法·机器学习·启发式算法