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)
相关推荐
小南家的青蛙8 分钟前
LeetCode LCR 085 括号生成
算法·leetcode·职场和发展
jackzhuoa14 分钟前
Rust 异步核心机制剖析:从 Poll 到状态机的底层演化
服务器·前端·算法
夜晚中的人海15 分钟前
【C++】模拟算法习题
c++·算法·哈希算法
花月C20 分钟前
算法 - 差分
人工智能·算法·机器学习
拆房老料20 分钟前
深入解析提示语言模型校准:从理论算法到任务导向实践
人工智能·算法·语言模型
晨非辰38 分钟前
《数据结构风云》递归算法:二叉树遍历的精髓实现
c语言·数据结构·c++·人工智能·算法·leetcode·面试
Dream it possible!43 分钟前
LeetCode 面试经典 150_链表_LRU 缓存(66_146_C++_中等)(哈希表 + 双向链表)
c++·leetcode·链表·面试
_dindong3 小时前
牛客101:二叉树
数据结构·c++·笔记·学习·算法
数字化脑洞实验室4 小时前
如何理解不同行业AI决策系统的功能差异?
大数据·人工智能·算法
前端架构师-老李6 小时前
从大厂到中小公司,活下去的五个生存法则
职场和发展