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)
相关推荐
plus4s6 小时前
2月12日(70-72题)
算法
m0_672703316 小时前
上机练习第24天
算法
逆境清醒6 小时前
软考高项常见英语词汇(2)
职场和发展·信息系统项目管理师·英语·计算机软考
edisao6 小时前
序幕-内部审计备忘录
java·jvm·算法
shehuiyuelaiyuehao6 小时前
22Java对象的比较
java·python·算法
Dev7z7 小时前
滚压表面强化过程中变形诱导位错演化与梯度晶粒细化机理的数值模拟研究
人工智能·python·算法
吴秋霖7 小时前
apple游客下单逆向分析
python·算法·逆向分析
YunchengLi9 小时前
【计算机图形学中的四元数】2/2 Quaternions for Computer Graphics
人工智能·算法·机器学习
CUC-MenG10 小时前
Codeforces Round 1079 (Div. 2)A,B,C,D,E1,E2,F个人题解
c语言·开发语言·数学·算法
666HZ66610 小时前
数据结构4.0 串
c语言·数据结构·算法