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)
相关推荐
DoraBigHead1 小时前
小哆啦解题记——异位词界的社交网络
算法
木头左2 小时前
逻辑回归的Python实现与优化
python·算法·逻辑回归
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
web_Hsir8 小时前
vue3.2 前端动态分页算法
前端·算法
地平线开发者10 小时前
征程 6M 部署 Omnidet 感知模型
算法·自动驾驶
秋说11 小时前
【PTA数据结构 | C语言版】线性表循环右移
c语言·数据结构·算法
浩瀚星辰202411 小时前
图论基础算法:DFS、BFS、并查集与拓扑排序的Java实现
java·算法·深度优先·图论
古希腊被code拿捏的神12 小时前
【Flutter】面试记录
flutter·面试·职场和发展
JiaJZhong13 小时前
力扣.最长回文子串(c++)
java·c++·leetcode
oioihoii14 小时前
C++随机打乱函数:简化源码与原理深度剖析
开发语言·c++·算法