446. 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]
Approach #1: DP. [C++]
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
if (A.size() == 0) return 0;
vector<map<int, int>> dp(A.size()+1);
int res = 0;
for (int i = 0; i < A.size(); ++i) {
for (int j = 0; j < i; ++j) {
long dif = (long)A[i] - A[j];
if (dif < INT_MIN || dif > INT_MAX) continue;
int d = (int)dif;
dp[i][d] += 1;
if (dp[j].find(d) != dp[j].end()) {
dp[i][d] += dp[j][d];
res += dp[j][d];
}
}
}
return res;
}
};
Analysis:
1. res is the final count of all valid arithmetic subsequence slices;
2. dp will store the intermediate results [i, [dif, count]], with i indexed into the array and dif as the key. count is the number of result with the intermediate results.
3. for each index i, we find the total number of "generalized" arithmetic subsequence slices ending at it with all possible differences. This is done by attaching A[i] to all slices of dp[j][d] with j less than i.
4. Within the inner loop, we first use a long variable diff to filter out invalid cases, then get the count of all valid slices (with element >= 3) as dp[j][d] add it to the final count. At last we update the count of all "generalized" slices for dp[i][d] by adding the two parts together: the orginal value of dp[i][d], the counts from dp[j][d].
Reference:
https://leetcode.com/problems/arithmetic-slices-ii-subsequence/
446. Arithmetic Slices II - Subsequence的更多相关文章
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
- 446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { ...
- 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)
Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...
- 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] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
随机推荐
- MS SQL 流程控制语句
Declare myCursor cursor For Select * from table1 open myCursor Fet ...
- 5 MySQL--表--数据类型
存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/mysql/mysql-data-type ...
- KEY操作续
[KEY操作续] 1.UMP key 序列化给定 key ,并返回被序列化的值,使用 RESTORE 命令可以将这个值反序列化为 Redis 键. 序列化生成的值有以下几个特点: 它带有 64 位的校 ...
- Graphics.BlitMultiTap解析
[Graphics.BlitMultiTap解析] 上述代码的四个偏移,表示利用此4个偏移,生成4张纹理单位.下面每一个SetTexture,默认会调用一个纹理单位. 而在Shader中,Unity会 ...
- Simple Style
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- Golang之匿名函数和闭包
Go语言支持匿名函数,即函数可以像普通变量一样被传递或使用. 使用方法如下: main.go package main import ( "fmt" ) func main() { ...
- JavaScript stringObject.replace() 方法
定义和用法: replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法: stringObject.replace(RegExp/substr,reol ...
- VGGNet
VGGNet 是牛津大学计算机视觉组(Visual Geometry Group)和 GoogleDeepMind 公司的研究员一起研发的的深度卷积神经网络. 在ImageNet大型视觉识别挑战 IL ...
- SetupDiEnumDeviceInfo
BOOLEANSetupDiEnumDeviceInfo(IN HDEVINFO DeviceInfoSet,IN DWORD MemberIndex,OUT PSP_DEVINFO_DATA Dev ...
- matplotlib安装错误依赖问题解决
When install "matplotlib" with "pip", if you get the following error, it means t ...