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] 的最长回文子序列长度,那么状 ...
随机推荐
- 为什么Netty这么火?与Mina相比有什么优势?
Netty是什么?为什么这么火? Netty是目前最流行的由JBOSS提供的一个Java开源框架NIO框架,Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服 ...
- react-router踩坑
1.当用BrowserRouter时,<Link/>组件必须放在BrowserRouter里,如果是存在于某个组件里的Link,则该组件也必须放在Router里,不然会出现url改变页面为 ...
- 配置 jaco机械臂 ros环境
---恢复内容开始--- 终于有机械臂了, 首先先下载包 cd ~/catkin_ws/src git clone https://github.com/Kinovarobotics/kinova-r ...
- RHEL5/6/7中常用命令及命令之间的差异
System basics Task RHEL5 RHEL6 RHEL7 View subscription information /etc/sysconfig/rhn/systemid /etc/ ...
- 4.Struts2中Action的三种访问方式
1.传统的访问方式-很少使用 通过<action>标签中的method属性,访问到action中的具体方法 具体实现: 1.action代码 import com.opensymphony ...
- H5调用腾讯地图
获取当前定位的经纬度并在容器内显示当前位置 (安卓上的位置有点偏差) 在vue的index.html中需要引用 template <div id="container" st ...
- 线程池 一 ForkJoinPool
java.util.concurrent public class ForkJoinPool extends AbstractExecutorService public abstract class ...
- VS 2019企业版激活码
Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DF
- delphi 单元 MSHTML 之Ihtmldocument2
delphi : Ihtmldocument2接口的利用 MSHTML是微软公司的一个COM组件,该组件封装了HTML语言中的所有元素及其属性,穿越其供给的规范接口,能够访问指定网页的所有元素. MS ...
- android 插件化框架VitualAPK
推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) LeakCanary 与 鹅场Matrix ResourceCa ...