A sequence of number 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 sequence:

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 slice of
that array is any pair of integers (P, Q) such that 0 <= P < Q
< N.

A slice (P, Q) of array A is called arithmetic if the sequence:
A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.

The function should return the number of arithmetic slices in the array A.

Example:

A = [1, 2, 3, 4]

return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.

Idea 1. How to extend the solution from A[i...j] to A[i...j+1]?  dynamic programming的应用解法,试着想如果A[i..j]是arithmetic sequence,dp[j]是以A[j]结尾的arithmetic sequence的个数,那么

  dp[j+1] = dp[j] + 1 if A[j+1] - A[j] = A[j] - A[j-1]; 
  dp[j+1] = 0 otherwise;
[1, 2, 3, 4, 5], 以3结尾的arithmetic sequence(1, 2, 3)是 1,
      以4结尾的 arithmetic sequence有(2, 3, 4), (1, 2, 3, 4)
                以5结尾的 arithmetic sequence有(3, 4, 5), (2, 3, 4, 5), (1, 2, 3, 4, 5)
 class Solution {
public int numberOfArithmeticSlices(int[] A) {
int count = 0;
int curr = 0;
for(int i = 2; i < A.length; ++i) {
if(A[i-1] - A[i-2] == A[i] - A[i-1]) {
++curr;
count += curr;
}
else {
curr = 0;
}
}
return count;
}
}

python:

 class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
curr: int = 0
count: int = 0 for i in range(2, len(A)):
if A[i-1] - A[i-2] == A[i] - A[i-1]:
curr += 1
count += curr
else:
curr = 0 return count

Idea 2. Instead of updating the accumulated sum for each consecutive sequence, only updating the sum once the longest sequence so far is found. 根据公式 curr * (curr+1)/2来计算现在这一段的arithmetic sequence的个数.

[1, 2, 3, 4, 5], 以3结尾的arithmetic sequence个数是 1,(1, 2, 3)
      以4结尾的 arithmetic sequence个数是 2,有(2, 3, 4), (1, 2, 3, 4)
                以5结尾的 arithmetic sequence个数是 3,有(3, 4, 5), (2, 3, 4, 5), (1, 2, 3, 4, 5)

总个数 1 + 2 + 3 = (1+3)*3/2 = 6

 class Solution {
public int numberOfArithmeticSlices(int[] A) {
int count = 0;
int curr = 0;
for(int i = 2; i < A.length; ++i) {
if(A[i-1] - A[i-2] == A[i] - A[i-1]) {
++curr;
}
else {
count += curr * (curr + 1)/2;
curr = 0;
}
} count += curr * (curr + 1)/2;
return count;
}
}

python:

 class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
curr: int = 0
count: int = 0 for i in range(2, len(A)):
if A[i-1] - A[i-2] == A[i] - A[i-1]:
curr += 1
else:
count += curr * (curr + 1)/2
curr = 0 count += curr * (curr + 1)/2
return int(count)

Arithmetic Slices LT413的更多相关文章

  1. Arithmetic Slices II - Subsequence LT446

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

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

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

  3. [LeetCode] Arithmetic Slices 算数切片

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

  4. [LeetCode]413 Arithmetic Slices

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

  5. 413. Arithmetic Slices

    /**************************Sorry. We do not have enough accepted submissions.*********************** ...

  6. LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

    1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if th ...

  7. Leetcode: Arithmetic Slices

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

  8. [Swift]LeetCode413. 等差数列划分 | Arithmetic Slices

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

  9. LeetCode——Arithmetic Slices

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

随机推荐

  1. cdh 安装系列2--cdh manager product 安装

    前期准备工作: 准备三台centos7 地址分别为 192.168.20.163:192.168.20.162:192.168.20.161 用163这台机器链接外网,并安装manager以及mana ...

  2. JAVA声明一个对象数组

    Student stu[]=new Student[N]; Student stu={new Student(),~~~}; JAVA类型转换 String转为float String转为INT

  3. Numpy:ndarray数据类型和运算

    Numpy的ndarray:一种多维数组对象 N维数组对象,该对象是一个快速而灵活的大数据集容器,nadarry是一个通用的同构数据多维容器,也就是说,其中的所有元素必须是相同类型的.每个数组都有一个 ...

  4. E-R图学习笔记

    E-R图也称实体-联系图(Entity Relationship Diagram),提供了表示实体类型.属性和联系的方法,用来描述现实世界的概念模型.   方法 编辑 E-R方法是“实体-联系方法”( ...

  5. 在 MySQL 中创建一个中文数据库

    安装完 MySQL 后,要修改密码. step 1: SET PASSWORD = PASSWORD('your new password'); step 2: ALTER USER 'root'@' ...

  6. jquery使用FormData提交数据

    在jquery中,使用ajax提交表单数据. FormData可以很方便地获取到表单中的所有数据. 注意: ajax中的data参数为FormData类型时,contentType就不要设置成appl ...

  7. 侯捷STL课程及源码剖析学习3: 深度探索容器list

    一.容器概览 上图为 GI STL 2.9的各种容器.图中以内缩方式来表达基层与衍生层的关系.所谓的衍生,并非继承(inheritance)关系,而是内含(containment)关系.例如 heap ...

  8. RxJS之Subject主题 ( Angular环境 )

    一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...

  9. C语言之栈区、堆区

    一 局部变量存放在栈区中,函数调用结束后释放内存空间. #include "stdio.h"; #include "stdlib.h"; int *getNum ...

  10. angular实现链接锚记

    前言: 之所以这么说,是因为angular的路由将html默认的链接锚记的#给占用了,所以传统的链接锚记在这里将不再适用,这个有点坑啊,又要多写好几行代码来模拟这个功能. 实现原理: location ...