第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)
题意:给一个长度1000内的整数数列,求有多少个等差的子数列。
如 【2,4,6,8,10】有7个等差子数列。
想了一个O(n^2logn)的DP算法 DP[i][j]为 对于原数列中的Ai到Ai为止 公差为j的数列的个数, 显然公差范围很大需要用到map(所以有了Logn)然而这道题用BST的map是要TLE或者MLE的
而HASH_MAP又无法使用,所以只好自己写一个hash了。
看了网上的一些解答 都是用的python 这道题用cpp还是有点麻烦。
关于动规方程 就不具体给出了 说一下思路
假设当前递推到了原数列的 Ai和Aj且Aj-Ai =dif
那么 以Ai为结尾且公差为dif的数列的情况我们是已经知道的,所以可以在O(n^2)的时间内求解。
下面给出cpp的代码
讲道理,调试Hash函数是坠痛苦的
int len2[1000][1500];
long long int num[1000][1500];
class Solution {
public:
int hash(int lo,long long x,long long num[][1500])
{
long long begin=x;
x=(x%1007+1007)%1007;
while(true)
{ if(num[lo][x]==0||num[lo][x]==begin){num[lo][x]=begin;return x;}
else x=(x+1);
}
}
int numberOfArithmeticSlices(vector<int>& A) {
int ans=0; memset(len2,0,sizeof(len2));
memset(num,0,sizeof(num));
int* lasp;
int *nowp;
for(int i=0;i<A.size();i++)
{ for(int j=i+1;j<A.size();j++)
{
long long int dif=(long long)A[j]-(long long)A[i];
int loci=hash(i,dif,num);
int locj=hash(j,dif,num);
lasp=&len2[i][loci];
int sum=*lasp;
nowp=&len2[j][locj];
*nowp+=sum+1;
ans+=sum;
}
} return ans;
}
};
第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)的更多相关文章
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
- 446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- 446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { ...
- Arithmetic Slices II - Subsequence LT446
446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- Leetcode: Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [Swift]LeetCode446. 等差数列划分 II - 子序列 | Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
随机推荐
- Java 文件操作大集合
package com.sess.hny.sys.fileserver; import java.io.BufferedInputStream;import java.io.BufferedOutpu ...
- assert.notDeepEqual()
assert.notDeepEqual(actual, expected[, message]) 深度地不相等比较测试,与 assert.deepEqual() 相反. const assert = ...
- Python之面向对象函数式编程
Python之面向对象函数式编程 函数式编程的根本就是用 def 去模拟数学式的编程逻辑. 类似与 y = 2*x + 1 ,当x = 3 时,函数的结果y就得7. def test(x): retu ...
- 高阶函数 map,reduce, filter的用法
1. map 用法 def fun_C(x): """求平方""" return x ** 2 result = map(fun_C, my ...
- STM32F407 串口通信:分类&常见接口 个人笔记
串行通信的分类 常见串行通信接口 STM32 UART STM32串口异步通信需要定义的参数: ① 起始位 ② 数据位(8位或者9位) ③ 奇偶校验位(如果有的话是第9位) ④ 停止位(1,15,2位 ...
- Node.js & module system
Node.js & module system Node.js v10.9.0 Documentation https://nodejs.org/api/modules.html#module ...
- mysql启服务的时候报1067错误解决办法
网上百度了半天没有有效的办法,推荐一个万能的办法: 1.看日志: mysql安装目录下 data文件 -> 后缀为.err 的文件就是日志文件 打开它 : 可以看到错误信息 2.看错 ...
- hdu 2433 Travel (最短路树)
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...
- 共享一个NOI用过的vimrc [rc][vimrc]
set nocp set nu set ru set noet set ai set cin set mouse =a set mp=g++\ %\ -o\ %<\ -g\ -Wall\ -Ws ...
- 【BZOJ2301】Problem b(莫比乌斯反演)
题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d, 且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000 ...