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)
相关推荐
青 春 记 忆2 小时前
LeetCode 53. 最大子数组和|Python 解法详解
python·算法·leetcode
豆瓣鸡2 小时前
算法日记 - Day10
算法
戴西软件2 小时前
戴西CAxWorks.VPG车辆工程仿真软件技术解析(上)——安全仿真体系的自动化构建
运维·网络·数据库·人工智能·算法·安全·自动化
zander2582 小时前
4. 寻找两个正序数组的中位数:用分割点代替合并
数据结构·算法
Kisorge2 小时前
【电机控制器】 基于STSPIN32G4的FOC控制
stm32·嵌入式硬件·算法
Elivs3 小时前
RMSNorm函数
人工智能·算法·机器学习
tudousisi2223 小时前
P4447 [AHOI2018初中组] 分组 题解复盘
算法
SNAKEpc121383 小时前
OpenGL(十一)- 变换管线
c语言·c++·算法·矩阵·图形渲染
饼饼学习空间智能5 小时前
遥操作数据能否提升机器人自主化水平?详解模仿学习、仿真放大与Sim2Real闭环
人工智能·算法