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)
相关推荐
Touper.3 分钟前
JavaSE -- 泛型详细介绍
java·开发语言·算法
sun0077005 分钟前
std::forward作用
开发语言·c++·算法
JoernLee23 分钟前
机器学习算法:支持向量机SVM
人工智能·算法·机器学习
V我五十买鸡腿32 分钟前
顺序栈和链式栈
c语言·数据结构·笔记·算法
我爱一条柴ya1 小时前
【AI大模型】线性回归:经典算法的深度解析与实战指南
人工智能·python·算法·ai·ai编程
三维重建-光栅投影2 小时前
VS中将cuda项目编译为DLL并调用
算法
课堂剪切板5 小时前
ch03 部分题目思路
算法
山登绝顶我为峰 3(^v^)36 小时前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Two_brushes.7 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
森焱森9 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机