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] 的最长回文子序列长度,那么状 ...
随机推荐
- 新建pc端页面的模板
pc端页面,要做兼容.新建pc端模板时,先要初始化浏览器的样式,我命名为reset.css @charset "utf-8"; /* 取消链接高亮 */ body,div,ul,l ...
- Tomcat Architect
Tomcat Architect Hierarchy of nested tag representing different components in server.xml. 1 <Serv ...
- 使用wireshark在windows平台下捕获HTTP协议数据包中的帐号密码信息
1.打开wireshark软件,从Interface List中选择相应的网卡,例如我的PC机上是“本地连接”,然后选择”Start”启动抓包程序. 2.打开学校主页,输入账号和密码登录校内邮箱. 3 ...
- vSphere ESXi如何使用嵌套虚拟化(1-vSphere 6.0+linux虚拟化)
今天准备在vSphere当中上传一个用workstation做出来的虚拟机,没想到遇到了很多问题,在这里,作为新手小白碰到了一些问题,在这里总结一下. 1.workstation做出来的虚拟机直接上传 ...
- nginx : OpenEvent Faied access is denied
主要是naginx做成服务后, 服务运行的用户权限不够(他是system的),而cmd运行的是(Administrator) 所以我们去服务里,修改成以下就好了
- jq-demo-楼梯效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- es6学习(一)
之前一直听说es6,断断续续看过阮一峰老师的"ECMAScript 6 入门",觉得写得很好,但由于实际项目中没用到,所以并没有过多的去研究(人的惰性在我这里表现的淋漓尽致).直到 ...
- python 相关操作
json转字典: #如:{"Message":"OK","RequestId":"9922A379-7373-492C-842A- ...
- jQuery - DOM相关
1. 操作文本 console.log($("#t1").html()); // 获取span元素中的内容, 包含html标签 $("#t1").html(&q ...
- ArrayList集合二
集合的遍历 通过集合遍历,得到集合中每个元素,这是集合中最常见的操作.集合的遍历与数组的遍历很像,都是通过索引的方式,集合遍历方式如下 13 import java.util.ArrayList; 1 ...