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. Python之爬虫-中国大学排名

    Python之爬虫-中国大学排名 #!/usr/bin/env python # coding: utf-8 import bs4 import requests from bs4 import Be ...

  2. 91-Williams' Percent Range 威廉指标.(2015.7.4)

    Williams' Percent Range 威廉指标 ~计算: %R = (HIGH(i-n)-CLOSE)/(HIGH(i-n)-LOW(i-n))×100 注解:CLOSE: 当前时段的收盘价 ...

  3. React & search & keyboard ghost

    React & search & keyboard ghost DOM events https://www.w3schools.com/jsref/dom_obj_event.asp ...

  4. 看板娘 & 二次元 & live2d

    live2d https://l2dwidget.js.org/dev.html https://github.com/xiazeyu/live2d-widget.js 看板娘 要切换看板娘吗? ht ...

  5. [luoguP1877] [HAOI2012]音量调节(DP)

    传送门 绝世傻DP #include <cstdio> #include <iostream> #define N 51 int n, s, mx; bool f[N][100 ...

  6. [BZOJ3751][NOIP2014]解方程(数学相关+乱搞)

    题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...

  7. [K/3Cloud] 在插件中为辅助资料赋值

    因为辅助资料其实是一种特殊的基础资料,其赋值方法跟基础资料类似 this.Model.SetItemValueByNumber("FAssistant1", "Ameri ...

  8. 【ZJOI2017 Round1后记】

    2017.4.1: NOIP+Round1综合成绩出来,标准分离续命线差了80分,果然还是联赛坑挖太大了…… 不管怎么说能续命的话还是要试一下的…… 发毒誓:Round2前不打手游,不看NGA,不看星 ...

  9. POJ 2104 K-th Number (可持久化线段树)

    题目大意 给一个长度为n的序列,有m个询问,每次询问一个区间里面第k小的数. 解题分析 静态的区间第k大.复习了一下可持久化线段树. 首先对数值离散化,建一颗权值线段树.按照序列的顺序依次插入,每一个 ...

  10. HDU——1281 棋盘游戏

    棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...