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 ...
随机推荐
- 用VB实现COM+组件配置
在Windwos2000的管理工具里有一个“组件服务”工具,可以实现对COM+组件的应用的安装.启动.删除和对组件的安装.删除.这在安装一个有 COM+组件的应用系统时时非常有用的,我们可以通过程序控 ...
- Spring cloud config-client 爬坑
配置文件 找不到属性 Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'from' in st ...
- maven的pom报plugins缺失的解决方法
maven的pom报plugins却是的解决方法. 引用 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom: ...
- Redis Set命令
[Redis Set命令] SET key value [EX seconds] [PX milliseconds] [NX|XX] 将字符串值 value 关联到 key . 如果 key 已经持有 ...
- S EAI 客户主数据导入_test(detail)
一. 客户主数据模板导出 客户主数据和新联系人导入 Account_Config.ini文件 [Public] ConnectString=host="siebel://10.10.1.15 ...
- VMware Workstation 虚拟机的服务启动项
- react native 导航路由组件react-navigation的使用
navigation的几个难点和问题: 1.底部tab是否可以加上中间的大按钮? 如果加上,如何触发事件? js文件放哪? 2.navigation的登录注册页面.成功后应该不能返回刚刚的登录页面?清 ...
- Kafka学习整理五(Consumer配置)
Property Default Description group.id 用来唯一标识consumer进程所在组的字符串,如果设置同样的group id,表示这些processes都是属于同一个 ...
- How To Debug Qmake Pro File
对于程序代码,我们经常使用到调试. 可是,对于有些项目的配置文件,比如Qt的Pro文件, 一个项目复杂的话,Pro文件就很容易出错. 此时的Pro文件,如果也能调试的话,那么是十分的快捷方便的. 解决 ...
- A simple way to crack VBA password in Excel file
Unbelivibale, but I found a very simple way that really works! Do the follwoing: 1. Create a new sim ...