第六周 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 ...
随机推荐
- xphrof性能分析线上部署实践
说明 将xhprof部署在线上环境,在特定情况下进行性能分析,方便快捷的排查线上性能问题. 通过参数指定及添加代码行触发进入性能分析,并将结果保存入MongoDB. 因为xhprof对性能的影响,只部 ...
- python3爬虫-爬取58同城上所有城市的租房信息
from fake_useragent import UserAgent from lxml import etree import requests, os import time, re, dat ...
- STL优先队列重载
priority_queue默认是大根堆,如果需要使用小根堆,如下 int main(){ priority_queue<int,vector<int>,greater<int ...
- [转]MySQL5字符集支持及编码研究
前言 在更新数据库时,有时会遇到这样的错误: Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8_general_ci,COER ...
- 2018/3/4 Activiti教程之对于流程的基本操作以及从发起到完成还有相关注意事项(与Springboot整合版)三
写教程实在太累了,,,还浪费时间,Activiti教程就写到这好了,不过最近在玩区块链,到时候写几个区块链方面的教程. 这是一些流程的查询与删除api,删除这块,默认是级联删除(加个false参数,就 ...
- 移动端click事件延迟300ms该如何解决
window.addEventListener( "load", function() { FastClick.attach( document.body ); }, fa ...
- Free Goodies UVA - 12260
Petra and Jan have just received a box full of free goodies, and want to divide the goodies between ...
- Linux简单口令
创建文件1.touch2.echo "">>file_name3.vim 文件名创建文件夹1.mkdir -p /abc/cc/bbb删除文件rm -f 文件删除文件夹 ...
- Java使用Memcached和Redis简单示例
package xmq.study.memcached; import java.io.IOException; import java.net.InetSocketAddress; import n ...
- 8VC Venture Cup 2016 - Final Round (Div2) E
贪心.当前位置满油可达的gas station中,如果有比它小的,则加油至第一个比他小的.没有,则加满油,先到达这些station中最小的.注意数的范围即可. #include <iostrea ...