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)
相关推荐
chh56311 分钟前
C++--list
开发语言·数据结构·c++·学习·算法·list
killerbasd18 分钟前
总结 7。10
人工智能·算法·机器学习
林间码客27 分钟前
RAG系统评估指南:从入门到实践
人工智能·算法·机器学习
凌波粒30 分钟前
LeetCode--47.全排列 II(回溯算法)
算法·leetcode·职场和发展
学究天人32 分钟前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷8)
线性代数·算法·机器学习·数学建模·动态规划·图论·抽象代数
ctrl_v助手37 分钟前
HALCON 学习笔记1
笔记·数码相机·学习·算法·计算机视觉
ShallWeL2 小时前
【机器学习】(16)—— 数值数据
人工智能·python·算法·机器学习·数据分析
hongyucai2 小时前
详解rlinf强化学习四步曲
人工智能·python·算法·架构
变量未定义~2 小时前
ST表-龙骑士军团【算法赛】
数据结构·算法