当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- What are the test sites for tunnel engineering?
- Function of activation function
- 公司視頻加速播放
- G - Supermarket
- Seven imperceptible truths in software testing
- 职场进阶指南:大厂人必看书籍推荐
- Linux regularly backs up MySQL database
- Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
- GTSAM中李群的运用
- [C language] string left rotation
猜你喜欢
VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
【Postman】测试(Tests)脚本编写和断言详解
G - Supermarket
[API interface tool] Introduction to postman interface
自定义指定路由上的Gateway过滤器工厂
P问题、NP问题、NPC问题、NP-hard问题详解
假设检验学习笔记
LeetCode 729. 我的日程安排表 I
[postman] collections configuration running process
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
随机推荐
F - True Liars (种类并查集+DP)
E - food chain
Overview of three core areas of Mathematics: algebra
對數據安全的思考(轉載)
[C language] string left rotation
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
[wechat applet] build a development tool environment
Cognitive introspection
Resttemplate and feign realize token transmission
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
Manhattan distance and Manhattan rectangle - print back font matrix
properties文件
【Postman】Monitors 监测API可定时周期运行
功能安全之故障(fault),错误(error),失效(failure)
Interface test: what are the components of the URL in fiddler
Detailed explanation of P problem, NP problem, NPC problem and NP hard problem
IDEA 新UI使用
对数据安全的思考(转载)
B - The Suspects
全链路压测:构建三大模型