leetcode-157周赛-5214-最长定差子序列
题目描述:

class Solution:
def longestSubsequence(self, arr: List[int], difference: int) -> int:
dp = dict()
for a in arr:
pre = a - difference
if pre in dp:
dp[a] = max(dp.get(a, 0), dp[pre] + 1)
else:
dp[a] = 1
return max([x for _, x in dp.items()])
优化:
class Solution(object):
def longestSubsequence(self, A, D):
count = collections.defaultdict(int)
for x in A:
#prev + d = x
# prev = x-d
count[x] = max(count[x], count[x-D] + 1)
return max(count.values())
leetcode-157周赛-5214-最长定差子序列的更多相关文章
- LeetCode 5214. 最长定差子序列(Java)HashMap
题目: 5214. 最长定差子序列 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等 ...
- leetcode 1218. 最长定差子序列
问题描述 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等差子序列的长度. 示例 ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] Longest Harmonious Subsequence 最长和谐子序列
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- LeetCode 516——最长回文子序列
1. 题目 2. 解答 与最长回文子串类似,我们可以用动态规划来求解这个问题,只不过这里的子序列可以不连续.我们定义状态 state[i][j] 表示子串 s[i, j] 的最长回文子序列长度,那么状 ...
随机推荐
- Linux 进程间通信 消息队列 实现两个进程间通信
例子: 通过消息队列实现两个进程间通信,一个进程从终端输入数据,通过消息队列发送,另一个进程通过消息队列接收数据 文件1 创建进程1 终端输入通过消息队列发送数据 #include <stdio ...
- C#获取本月开始日期和结束日期
DateTime dt = DateTime.Now; //本月第一天时间 DateTime dt_First = dt.AddDays( - (dt.Day)); //获得某年某月的天数 int y ...
- Struts2.xml的配置
框架初始知识: Servlet VS Filter Filter的性能更强,因为Servlet能实现的,Filter都能实现. Filter还有拦截资源的作用 是Servlet所不能实现的. F ...
- 【BZOJ 3569】DZY Loves Chinese II
题面 Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图 ...
- SolidWorks直线命令快捷转换为圆弧命令
在进行草图绘制的时候,有时候需要切换到圆弧命令,此时来回切换比较麻烦, 我们可以将鼠标回碰线段起点,此时便成为了圆弧工具. 再次回碰,可改变圆心方向 利用鼠标操作,快捷切换绘图工具.
- Java中volatile关键字及其作用是什么?
在 Java 多线程中如何保证线程的安全性?那我们可以使用 Synchronized 同步锁来给需要多个线程访问的代码块加锁以保证线程安全性.使用 synchronized 虽然可以解决多线程安全问题 ...
- leetcood学习笔记-54-螺旋矩阵
题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0 ...
- react使用阿里爸爸的iconfont时,不展示的问题
选择使用Unicode时: 正常使用如下,显示也是正常: <i className="iconfont"></i> 使用map去循环时,需将原本的,改成 ...
- web音乐播放器
今天闲暇时间,花了2小时,写了个简单音乐播放器.欢迎大家来吐糟 先看下界面截图 大体实现:播放,停止,上一曲,下一曲,循环播放功能. 知识点:1.html 中audio 2.css 位置fixed 其 ...
- python virtual env 使用 jupyter ipython notebook,舒服了, 工作效率翻倍
话不多说,尊重原作者 知乎链接