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 ...
随机推荐
- 「小程序JAVA实战」小程序首页视频(49)
转自:https://idig8.com/2018/09/21/xiaochengxujavashizhanxiaochengxushouyeshipin48/ 视频显示的内容是视频的截图,用户的头像 ...
- delphi加密算法
function EncryptKey(sUser,sPasswd:string):string;stdcall; var __i,__k,__a : Integer; __s : string; b ...
- 新手C#重载、重写的学习2018.08.04
重载:在同一类(class)中,使用相同的方法名称,不同的参数和(不一定)不同的返回值类型构造成的方法. 举例: class OverLoadTest { public void Hello() { ...
- 介绍MVC编程架构模式
MVC(Model/View/Controller)模式是国外用得比较多的一种框架模式,最早是在Smaltalk中出现.MVC包括三类对象. Model——是应用对象 View——是它在屏幕上的表示 ...
- 18.4Sum (Map)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- centos7 ntp服务器配置
一.ntp服务是什么 1. 定义 NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议. 2. 发展 首次记载在Internet Enginee ...
- 解题报告-908. Smallest Range I
题目 : Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...
- boost之date_time库
最近开了boost库的学习,就先从日期时间库开始吧,boost的date_time库是一个很强大的时间库,用起来还是挺方便的.以下算是我学习的笔记,我把它记录下来,以后便于我复习和查阅. #inclu ...
- windows下使用redis
一.下载windows版本的Redis 官网只提供linux版本的下载 官网下载地址:http://redis.io/download Redis 没有官方的Windows版本,但是微软开源技术团队( ...
- msys2 + clion安装所需的mingw64编译环境
pacman -S mingw-w64-x86_64-toolchain 输出结果为: $ pacman -S mingw-w64-x86_64-toolchain:: 在组 mingw-w64-x8 ...