A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, these are arithmetic sequences:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9
The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N. A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2. The function should return the number of arithmetic subsequence slices in the array A. The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1. Example: Input: [2, 4, 6, 8, 10] Output: 7 Explanation:
All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]

参考了https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution

这道题DP思路还是能想出来,Time O(N^2), Space O(N^2)

T(i, d), which denotes the total number of arithmetic subsequence slices ending at index i with difference d. The base case and recurrence relation are as follows:

  1. Base case: T(0, d) = 0 (This is true for any d).
  2. Recurrence relation: T(i, d) = summation of (1 + T(j, d)) as long as 0 <= j < i && d == A[i] - A[j].

这个地方有个Corner case, [2,2,3,4,5], 到3的时候,前面有两个2,这个+1具体应该怎么处理,如果直接+1并且用T(i,d)表示total number of arithmetic subsequence slices ending at index i with difference d的话, 那么到3这个数的时候T(2,1)==2,那不岂是表示3这里有两个valid arithmetic subsequence? 而我们知道其实是0个。

所以我们稍作改变T(i,d)表示total number of “generalized” arithmetic subsequence slices ending at index i with difference d, 这个"generalized" slices允许长度为2,

比如上例[2,2,3,4,5], 考虑diff==1的情况,到3的时候generalized slices number是2, 分别是[2, 3], [2, 3]

到4的时候generalized slices number是3,分别是[2,3,4], [2,3,4], [3,4]

到5的时候generalized slices number是4, 分别是[2,3,4,5], [2,3,4,5], [3,4,5], [4,5]

如此错了一位,算result的时候也错一位算

这道题又是一道用HashMap来做DP的题,是因为diff大小不确定,没有range,像这种没有range的DP,用HashMap吧

另外语法注意第3行,等号后面map不能再有泛型;第9行等号后面一定要有long

 public int numberOfArithmeticSlices(int[] A) {
int res = 0;
Map<Integer, Integer>[] map = new Map[A.length]; for (int i = 0; i < A.length; i++) {
map[i] = new HashMap<>(i); for (int j = 0; j < i; j++) {
long diff = (long)A[i] - A[j];
if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) continue; int d = (int)diff;
int c1 = map[i].getOrDefault(d, 0); //orignial value of T(i, d)
int c2 = map[j].getOrDefault(d, 0); //the counts from T(j, d)
res += c2;
map[i].put(d, c1 + c2 + 1);
}
} return res;
}

Leetcode: Arithmetic Slices II - Subsequence的更多相关文章

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

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

  2. Arithmetic Slices II - Subsequence LT446

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

  3. LeetCode 446. Arithmetic Slices II - Subsequence

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

  4. 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)

    Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...

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

  6. LeetCode446. Arithmetic Slices II - Subsequence

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

  7. 446. Arithmetic Slices II - Subsequence

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

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

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

  9. [LeetCode] Arithmetic Slices 算数切片

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

随机推荐

  1. HDU 4750 Count The Pairs(并查集)

    题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...

  2. 腾讯 pc端面试(2015.10.26)

    整个面试过程全部围绕着我在前一家公司做过的项目开始提问.因为这个项目是我主要负责的,所以面试官第一个问题是让我讲解了整个项目的框架结构.在对于项目的框架结构的讲解方面,大致条理还算清醒但是因为很少对做 ...

  3. C#中Post和Get提交

    1.Post提交 private string PostWebRequest(string Url, string paramData, string dataEncode) { string ret ...

  4. Hashtable在ViewState中无法增加值

    在我调试程序的时候,我发现WebForm 2.0和MVC3解析ViewState的方式不同,同样的代码,在Weorm中管用,在MVC中不起作用. private Hashtable ht { get ...

  5. SpringMVC整合Shiro

    首先是web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&quo ...

  6. 采用DOM进行表格的修改操作

    2015-08-31 <html> <head> <title>采用DOM进行表格的修改操作</title> <script language=& ...

  7. jquery .post .get中文参数乱码解决方法详解

    jquery默认的编码为utf-8,做项目时有时处于项目需要用到ajax提交中文参数,乱码问题就很头疼了,折腾了许久终于弄出来了.为了便于传输,我们首先将需要用到的参数用javascript自带的函数 ...

  8. css3常用代码整理

    1.圆角 .rd10{-moz-border-radius: 10px;-webkit-border-radius: 10px;border-radius: 10px;-khtml-border-ra ...

  9. ArcGIS AddIN开发异常之--“ValidateAddInXMLTask”任务意外失败

     ArcGIS AddIN开发时,产生如下异常错误 2 “ValidateAddInXMLTask”任务意外失败.System.NullReferenceException: 未将对象引用设置到对象的 ...

  10. 20145337 《Java程序设计》第五周学习总结

    20145337 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 JAVA中的所有错误都会被包装成对象,如果你愿意,可以尝试执行并捕捉代表错误的对象后做一些处理.使用了try ...