Leetcode: Arithmetic Slices II - Subsequence

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的时候也错一位算

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

 1 public int numberOfArithmeticSlices(int[] A) {
 2     int res = 0;
 3     Map<Integer, Integer>[] map = new Map[A.length];
 4
 5     for (int i = 0; i < A.length; i++) {
 6         map[i] = new HashMap<>(i);
 7
 8         for (int j = 0; j < i; j++) {
 9             long diff = (long)A[i] - A[j];
10             if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) continue;
11
12             int d = (int)diff;
13             int c1 = map[i].getOrDefault(d, 0); //orignial value of T(i, d)
14             int c2 = map[j].getOrDefault(d, 0); //the counts from T(j, d)
15             res += c2;
16             map[i].put(d, c1 + c2 + 1);
17         }
18     }
19
20     return res;
21 }
时间: 2024-11-05 12:05:41

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

Arithmetic Slices II - Subsequence LT446

446. Arithmetic Slices II - Subsequence Hard 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,

LeetCode 446. Arithmetic Slices II - Subsequence

原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: 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, thes

LeetCode446. Arithmetic Slices II - Subsequence

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 follo

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

详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { public: int numberOfArithmeticSlices(vector<int>& A) { int res = 0, n = A.size(); vector<unordered_map<int, int>> dp(n); for (int i

[LeetCode] Arithmetic Slices 算数切片

A sequence of number 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 sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

Leetcode: Arithmetic Slices

A sequence of number 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 sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

1.题目大意 A sequence of number 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 sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The

413. Arithmetic Slices(LeetCode)

A sequence of number 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 sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

[leetcode]Math-413. Arithmetic Slices

A sequence of number 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 sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi