当前位置:网站首页>Simulation volume leetcode [general] 1218 Longest definite difference subsequence
Simulation volume leetcode [general] 1218 Longest definite difference subsequence
2022-07-06 06:18:00 【Encounter simulation volume】
1218. The longest definite difference subsequence
Give you an array of integers arr And an integer difference, Please find out and return to arr The length of the longest isochromatic subsequence in , The difference between adjacent elements in this subsequence is equal to difference .
Subsequence It means that without changing the order of the other elements , Remove from... By deleting some elements or not deleting any elements arr Derived sequence .
Example 1:
Input :arr = [1,2,3,4], difference = 1
Output :4
explain : The longest isochromatic subsequence is [1,2,3,4].
Example 2:
Input :arr = [1,3,5,7], difference = 1
Output :1
explain : The longest arithmetic subsequence is any single element .
Example 3:
Input :arr = [1,5,7,8,5,3,4,2,1], difference = -2
Output :4
explain : The longest isochromatic subsequence is [7,5,3,1].
Tips :
1 <= arr.length <= 105
-104 <= arr[i], difference <= 104
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def __init__(self):
""" Calculate to each number , How many long series are there in total . All from 0 Start calculating """
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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- 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
- Properties file
- E - 食物链
- 【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
- Function of activation function
- 把el-tree选中的数组转换为数组对象
- 【Postman】Collections-运行配置之导入数据文件
- F - true liars (category and search set +dp)
- Resttemplate and feign realize token transmission
- 通过修改style设置打印页样式
猜你喜欢

黑猫带你学UFS协议第4篇:UFS协议栈详解

Manhattan distance and Manhattan rectangle - print back font matrix

Function of activation function

IP day 16 VLAN MPLS configuration

Seven imperceptible truths in software testing

What are the test sites for tunnel engineering?

10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块

【无App Push 通用测试方案

Idea new UI usage

曼哈顿距离和-打印菱形
随机推荐
Properties file
【Postman】Monitors 监测API可定时周期运行
LeetCode 1200. 最小绝对差
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
单元测试的意义
Réflexions sur la sécurité des données (réimpression)
Request forwarding and redirection
技术分享 | 常见接口协议解析
RestTemplate、Feign实现Token传递
Manhattan distance sum - print diamond
使用Nacos管理配置
D - How Many Answers Are Wrong
模拟卷Leetcode【普通】1447. 最简分数
Application of Lie group in gtsam
Isam2 operation process
【API接口工具】postman-界面使用介绍
[postman] test script writing and assertion details
还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
对数据安全的思考(转载)
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能