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的更多相关文章

  1. LeetCode 446. Arithmetic Slices II - Subsequence

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

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

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

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

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

  4. Arithmetic Slices II - Subsequence LT446

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

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

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

  6. Leetcode: Arithmetic Slices II - Subsequence

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

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

  8. LeetCode446. Arithmetic Slices II - Subsequence

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

  9. [LeetCode] Arithmetic Slices 算数切片

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

随机推荐

  1. Java常用的转义字符

    以下为常用的转义字符对照表: 字母前面加上捺斜线"\"来表示常见的那些不能显示的ASCII字符.称为转义字符.如\0,\t,\n等,就称为转义字符. 转义字符 意义 ASCII码值 ...

  2. Elasticsearch 2.4.1 Bigdesk 插件安装

    简介: Elasticsearch 2.4.1 安装 bigdesk bigdesk 是一个 ES 集群监控工具,可以检测到集群状态.各节点信息,包括 JVM.Thread Pools.OS.Proc ...

  3. C语言链表实现

    #define _CRT_SECURE_NO_WARNINGS #include "stdio.h" #include "stdlib.h" typedef s ...

  4. 使用 phpStorm 开发

    苦恼蛋疼的曾哥工作室,让人痛不欲生,缓慢的同步速度,另人恼火的插件配置,让人疯狂的卡.简直是让人用了几天之后就不行了. 废话不多说,一款很好的php IDE. 1. phpStorm 下载 here ...

  5. LevelDB Log文件

    [LevelDB Log文件] log文件在LevelDb中的主要作用是系统故障恢复时,能够保证不会丢失数据.因为在将记录写入内存的Memtable之前,会先写入Log文件,这样即使系统发生故障,Me ...

  6. Android开发之通过包管理器获取安装应用信息

    最近在自己写一个APP,有一个模块需要获取手机应用的一些信息.坑还是有,但都基本踩过了,自己把他实现了出来,实现方法还是很需要掌握的.底部弹出的对话框中四个选项的实现不多做说明,主要讲讲如何获取这些安 ...

  7. Unity Shader-简单均值模糊

    http://blog.csdn.net/puppet_master/article/details/52547442 与Amplify中的Simple Blur例子实现一样

  8. Web页面显示日期和动态时刻脚本

    <script language="JavaScript" type="text/JavaScript">    <!--        tm ...

  9. Struts2,Spring3,Hibernate4整合--SSH框架

    Struts2,Spring3,Hibernate4整合--SSH框架(学习中) 一.包的导入 1.Spring包 2.Hibernate 包 3.struts 包 (还欠 struts2-sprin ...

  10. 关于anroid设置webview背景方法探讨(转)

    最近的项目中一直关于webView设置背景色问题在研究,最终找到了解决的方法. 基于我项目的需求,从服务端传过来的是带有标签的文本,如果使用textView会让整个布局显得很乱,里面的<img ...