Leetcode443

题意:给一个长度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)的更多相关文章

  1. LeetCode 446. Arithmetic Slices II - Subsequence

    原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...

  2. 446. Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  3. 446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { ...

  4. Arithmetic Slices II - Subsequence LT446

    446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...

  5. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  6. Leetcode: Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  7. [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 ...

  8. LeetCode446. Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  9. LeetCode 413 Arithmetic Slices详解

    这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...

随机推荐

  1. MySql 基础 基本使用方法

    安装MySQL linux安装:阿里云服务器ecs配置之安装mysqlwindows安装: 解压 管理员身份进cmd执行解压目录下的可执行文件 初始化 D:\mysql-8.0.12-winx64\m ...

  2. ASP.NET MVC的帮助类HtmlHelper和UrlHelper

    在ASP.NET MVC框架中没有了自己的控件,页面显示完全就回到了写html代码的年代.还好在asp.net mvc框架中也有自带的HtmlHelper和UrlHelper两个帮助类.另外在MvcC ...

  3. 【转】SQLServer连接字符串配置:MultipleActiveResultSets

    ADO.NET 1.x 利用SqlDataReader读取数据,针对每个结果集需要一个独立的连接.当然,你还必须管理这些连接并且要付出相应的内存和潜在的应用程序中的高度拥挤的瓶颈代价-特别是在数据集中 ...

  4. 详解SpringBoot 添加对JSP的支持(附常见坑点)

    序言: SpringBoot默认不支持JSP,如果想在项目中使用,需要进行相关初始化工作.为了方便大家更好的开发,本案例可直接作为JSP开发的脚手架工程 SpringBoot+War+JSP . 常见 ...

  5. tyvj1045 最大的算式

    描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号.例 ...

  6. 基于端口的信息探测-portscan-1.0

    http://www.tiaozhanziwo.com/archives/174.html

  7. Query on a string

    You have two strings SS and TT in all capitals. Now an efficient program is required to maintain a o ...

  8. [bzoj3527][Zjoi2014]力_FFT

    力 bzoj-3527 Zjoi-2014 题目大意:给定长度为$n$的$q$序列,定义$F_i=\sum\limits_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum\lim ...

  9. 洛谷—— P1690 贪婪的Copy

    https://www.luogu.org/problem/show?pid=1690 题目描述 Copy从卢牛那里听说在一片叫yz的神的领域埋藏着不少宝藏,于是Copy来到了这个被划分为个区域的神地 ...

  10. Ubuntu 16.04安装WebStorm

    前提:必须正确安装JDK. 下载: http://confluence.jetbrains.com/display/WI/WebStorm+EAP 或者下载历史版本:https://www.jetbr ...