LeetCode 446. Arithmetic Slices II - Subsequence
来源:互联网 发布:我国能源现状数据 编辑:程序博客网 时间:2024/05/17 20:35
Problem Statement
(Source) 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, 97, 7, 7, 73, -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
Example:
Input: [2, 4, 6, 8, 10]Output: 7Explanation: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]
Solution
Tags: Dynamic Programming
.
For all possible combinations of positions j
and i
with j < i
, choose A[i] - A[j]
as the difference of the Arithmetic Slice with the last two elements being A[j], A[i]
, add the number of Arithmetic Slice that can be produced by this condition, and update dp[i][A[i] - A[j]]
, where dp[i][x]
stores the number of points before position i
that have distance x
from A[i]
(Condition 1), or recursively have distances x
from those points meets Condition 1.
class Solution(object): def numberOfArithmeticSlices(self, A): """ :type A: List[int] :rtype: int """ res, n = 0, len(A) dp = [{} for i in xrange(n)] for i in xrange(1, n): for j in xrange(0, i): dist = A[i] - A[j] s = dp[j].get(dist, 0) + 1 dp[i][dist] = dp[i].get(dist, 0) + s res += (s-1) return res
Complexity Analysis:
- Time Complexity:
O(n2) . - Space Complexity:
O(n2) .
References:
(1) LeetCode Discussion.
- LeetCode 446. Arithmetic Slices II - Subsequence
- LeetCode 446. Arithmetic Slices II - Subsequence
- Leetcode-446. Arithmetic Slices II - Subsequence
- 446. Arithmetic Slices II - Subsequence
- 446. Arithmetic Slices II - Subsequence
- 446. Arithmetic Slices II - Subsequence***
- 446. Arithmetic Slices II - Subsequence
- Arithmetic Slices II - Subsequence
- [LeetCode]446. Arithmetic Slices II
- LeetCode 446. Arithmetic Slices II
- [Leetcode] 446. Arithmetic Slices II
- leetcode 446. Arithmetic Slices II
- [LeetCode] Arithmetic Slices II
- 446. Arithmetic Slices II
- 446. Arithmetic Slices II
- 446. Arithmetic Slices II
- 446. Arithmetic Slices II
- 446. Arithmetic Slices II
- linear convolution/periodic convolution/circular convolution
- Dlib 19.2+VS2015 人脸检测(非CMake)
- android 开发资源
- 简单的自定义View——表盘时钟
- dubbo 使用学习七(结果缓存)
- LeetCode 446. Arithmetic Slices II - Subsequence
- 9、(知识篇)JAVA反射(1)
- String,StingBuffer,StingBuilder
- [Python]DJango部署在Apache服务器配置示例
- 【快速幂模板】 nyoj102 次方求摸 420 p次方求和(快速幂+同余定理)
- 10、(知识篇)JAVA反射(2)
- 排序之归并排序
- HDU3749 Financial Crisis(点双连通分量+并查集)
- 06-图2 Saving James Bond - Easy Version (25分)