当前位置:网站首页>模拟卷Leetcode【普通】1218. 最长定差子序列
模拟卷Leetcode【普通】1218. 最长定差子序列
2022-07-06 06:15:00 【邂逅模拟卷】
1218. 最长定差子序列
给你一个整数数组 arr 和一个整数 difference,请你找出并返回 arr 中最长等差子序列的长度,该子序列中相邻元素之间的差等于 difference 。
子序列 是指在不改变其余元素顺序的情况下,通过删除一些元素或不删除任何元素而从 arr 派生出来的序列。
示例 1:
输入:arr = [1,2,3,4], difference = 1
输出:4
解释:最长的等差子序列是 [1,2,3,4]。
示例 2:
输入:arr = [1,3,5,7], difference = 1
输出:1
解释:最长的等差子序列是任意单个元素。
示例 3:
输入:arr = [1,5,7,8,5,3,4,2,1], difference = -2
输出:4
解释:最长的等差子序列是 [7,5,3,1]。
提示:
1 <= arr.length <= 105
-104 <= arr[i], difference <= 104
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
"""计算到每个数,累计有多少长的数列。所有都从0开始计算"""
pass
def longestSubsequence(self, arr: List[int], difference: int) -> int:
res_cnt = {
}
for num in arr:
res_cnt[num] = res_cnt.get(num-difference,0)+1
return max(res_cnt.values())
def test(data_test):
s = Solution()
return s.longestSubsequence(*data_test)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- 黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
- [wechat applet] build a development tool environment
- Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
- [postman] the monitors monitoring API can run periodically
- Isam2 and incrementalfixedlagsmooth instructions in gtsam
- Software test interview questions - Test Type
- Cognitive introspection
- Commodity price visualization
- E - food chain
- PAT(乙级)2022年夏季考试
猜你喜欢
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
Career advancement Guide: recommended books for people in big factories
[ram IP] introduction and experiment of ram IP core
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
[eolink] PC client installation
Postman核心功能解析-参数化和测试报告
[postman] test script writing and assertion details
GTSAM中李群的运用
调用链监控Zipkin、sleuth搭建与整合
随机推荐
假设检验学习笔记
浅谈专项测试之弱网络测试
JMeter做接口测试,如何提取登录Cookie
Request forwarding and redirection
What are the test sites for tunnel engineering?
2022 software testing workflow to know
技术分享 | 常见接口协议解析
Customize the gateway filter factory on the specified route
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
對數據安全的思考(轉載)
The latest 2022 review of "graph classification research"
全链路压测:构建三大模型
JWT-JSON WEB TOKEN
E - 食物链
Manhattan distance sum - print diamond
nodejs实现微博第三方登录
Fault, error, failure of functional safety
误差的基本知识
JWT-JSON WEB TOKEN
Function of activation function