当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Introduction to promql of # yyds dry goods inventory # Prometheus
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 【Postman】测试(Tests)脚本编写和断言详解
- 【C语言】字符串左旋
- 【Postman】Collections配置运行过程
- Resttemplate and feign realize token transmission
- How to extract login cookies when JMeter performs interface testing
- 「 WEB测试工程师 」岗位一面总结
- Embedded point test of app
- 曼哈顿距离与曼哈顿矩形-打印回字型矩阵
猜你喜欢
![Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)](/img/c9/56fba6054c91f090de31463a8b4705.jpg)
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)

Manhattan distance and Manhattan rectangle - print back font matrix

异常检测方法总结

Understanding of processes and threads
![Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)](/img/0a/054b994b29d4c50ede8b23514cf4ee.jpg)
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)

Detailed explanation of BF and KMP

ESP32 ESP-IDF看门狗TWDT

MySQL之数据类型

【Postman】Collections-运行配置之导入数据文件

【API接口工具】postman-界面使用介绍
随机推荐
Hypothesis testing learning notes
把el-tree选中的数组转换为数组对象
[eolink] PC client installation
Luogu p1460 [usaco2.1] healthy Holstein cows
Nodejs realizes the third-party login of Weibo
[leetcode] day96 - the first unique character & ransom letter & letter ectopic word
The latest 2022 review of "graph classification research"
曼哈顿距离与曼哈顿矩形-打印回字型矩阵
进程和线程的理解
nodejs实现微博第三方登录
Testing and debugging of multithreaded applications
leaflet 地图
properties文件
IPv6 comprehensive experiment
Significance of unit testing
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
What are the test sites for tunnel engineering?
Function of contenttype
公司視頻加速播放
[postman] test script writing and assertion details