-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[算法]寻找最长等差数列 #4
Comments
对于原始问题如果有好想法也可以说。 |
投降了。。。
|
像是个好主意:能稍微解释一下第 3 点么? @guofei9987 乍一想,n^2 个数统计众数的复杂度是 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
描述
例
背景
主要以
cc-数学兴趣班-幼儿园-安道尔
在群里提的 寻找素数等差数列 问题为背景。假设已经用筛法生成了一个需要的素数数组,发现在里面寻找等差数列并不容易,所以抽象出该问题。抛砖引玉
目前我能想到的方法:等差数列就是要找
a + k d
,现在我的想法就是暴力枚举a
和d
这两个参数。比如对于前例(记为
{n}
)我想找以 3 为首的数列,即
a = 3
,考虑数组{n-a}
让
d
枚举这其中的数字,比如d = 2
时,计算{ (n-a)/d }
然后再在里面找是否存在
0 ... k
这样的连续整数能构成一个等差数列。发现间断就可以存下最大k
,如果大于之前找到的最大k
,更新a
,d
,k
这三个参数。最后可以根据这三个参数输出结果。时间复杂度大约是O(n^3)
,空间复杂度O(1)
。大家如果有其它算法可以交流下 :)
The text was updated successfully, but these errors were encountered: