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. Java进阶 线程安全

    多线程编程中的三个核心概念 原子性 这一点,跟数据库事务的原子性概念差不多,即一个操作(有可能包含有多个子操作)要么全部执行(生效),要么全部都不执行(都不生效). 关于原子性,一个非常经典的例子就是 ...

  2. linux suse 同步时间

    ntpdate 210.72.145.44 ip为中国(国家授时中心)

  3. cdnbest补充api

    1.应用防火墙---防CC 添加|修改 请求地址: {api_dir}/firewall/anticc 请求方式: PUT 请求参数: frcquency string 触发频率 例:low(低) | ...

  4. Javascript之基本类型和引用类型

    ECMAScript变量可能包含两种不同数据类型的值:基本类型值和引用类型值,基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 在将一个值赋给变量时,解析器必须确定这个值是基本 ...

  5. http协议与常见状态码

    什么是http: http是属于应用层(基于tcp的连接方式)的面向对象的协议,是计算机通过网络通信的规则,使得浏览器向web服务器请求信息和服务 http协议特点: http是一种无状态协议(对食物 ...

  6. 条件编译ifndef、ifdef、endif

    1.条件编译命令最常见的形式为: #ifdef 标识符 程序段1 #else 程序段2 #endif 当标识符已经被定义过(一般是用#define命令定义),则对程序段1进行编译,否则编译程序段2.  ...

  7. HDFS之深入简出(一)

    分布式文件系统HDFS 一:概述 1.HDFS设计目标 2.HDFS核心组件 3.HDFS副本机制 4.HDFS环境搭建 5.HDFS shell命令  java api 6.HDFS读写流程 7.H ...

  8. HDU-1002.大数相加(字符串模拟)

    本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...

  9. unity3d英语单词拼写小游戏Pics Quiz Maker With Categories 3.0

    下载地址: https://item.taobao.com/item.htm?spm=0.7095261.0.0.19f71debcef4hT&id=575991216080

  10. gdb打印C++容器

    将以下内容保存成 .gdbinit 文件放到你的根目录,或者在gdb中source这个文件可以加载. 直接print容器即可. # # STL GDB evaluators/views/utiliti ...