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] 的最长回文子序列长度,那么状 ...
随机推荐
- Vuex篇
[Vuex篇] vuex源码你学会了吗? 我来免费教你!~ 1.vuex是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的 ...
- python--reflect
一.反射 python 中用字符串的方式操作对象的相关属性,python 中一切皆对象,都可以使用反射 用eval 有安全隐患,用 反射就很安全 1.反射对象中的属性和方法 class A: a_cl ...
- 30个优秀的CSS技术和实例 By 彬Go 2008-12-04
在这里可发现很多与众不同的技术,比如:图片集.阴影效果.可扩展按钮.菜单等…这些实例都是使用纯CSS和HTML实现的.单击每个实例的标题可以被转向到该技术实例的相关教程或说明页面(英文),单击每个实例 ...
- Linux 下工作用户及环境
交叉工具的安装 工具链的编译过程请参考第三部分. 1. 下载交叉工具 2.95.3 下载地址:ftp://ftp.arm.linux.org.uk/pub/armlinux/toolchain/cro ...
- XOR linked list--- 异或链表
异或链表的结构 这是一个数据结构.利用计算机的的位异或操作(⊕),来降低双向链表的存储需求. ... A B C D E ... –> next –> next –> next –& ...
- cehsi
weibo https://oapi.dingtalk.com/robot/send?access_token=8c9ef96c99925383347c5f9f733ad6b8579c3f8ad072 ...
- wpf 绑定除数据上下文外的属性
例如: listview 绑定了一个windows.datacontext.一个集合,那么其中一个item想绑定windows.datacontext.A属性怎么办: 通过查找祖先的方法:
- bzoj1096题解
[解题思路] 预处理spi=∑pj(j∈[1,i]),si=si-1+(xi-xi-1)*spi-1表示把工厂1~i-1的产品都运到工厂i的花费.于是把工厂j+1~i的产品都运到工厂i的花费为si-s ...
- bzoj1024题解
[解题思路] 爆搜,状态f(r,x,y)表示剩下r刀,边长为x和y,对于每个状态枚举切成两块后的长度比或宽度比.复杂度o((n/2)n). [参考代码] #include <algorithm& ...
- NX二次开发-创建图纸尺寸表达式抑制UF_DRF_add_controlling_exp
#include <uf.h> #include <uf_modl.h> #include <uf_drf.h> #include <uf_obj.h> ...