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. zabbix 利用python脚本实现短信告警

    一.编写脚本 cd /usr/local/zabbix-4.0.3/share/zabbix/alertscripts vi zabbix_sms.py 内容如下: #!/usr/bin/python ...

  2. Real Time Render 4

    [Real Time Render 4] 1.Radiometry(辐射测试) deals with the measurement of electromagnetic(电磁) radiation( ...

  3. PHPActiveRecord 学习三

    #事务处理 注意事务 数据库要用InnoDB引擎 $c1 = User::connection(); try { //开启事务 $c1->transaction(); //sql语句 $sql ...

  4. as3.0控制声音大小

    //加载声音 var sound:Sound=new Sound(); sound.load(new URLRequest("1.mp3")): //声明声道 var soundC ...

  5. centos 6 和centos 7 系统下vnc配置

    一. VNC 服务的大概介绍: VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的. ...

  6. linux 切割文件的命令

    Head -1000 access.2016.log >> 10000_access.log

  7. Jmeter 录制脚本(一)

    第一种方法:使用Badboy来录制脚本 1. 启动Badboy, 工具栏上的红色圆形按钮是默认启动的,在地址栏直接输入被测试WEB项目的地址,然后点击右边的箭头. 2.录制完成后,点击工具栏上的黑色按 ...

  8. 惠普/aruba交换机

    1.开启CDP cdp run show cdp neighbors可看到各端口上.下联的AP或交换机 2.配置telnet管理 1)配置IP地址 vlan 77  name "VLAN77 ...

  9. 项目总结09:select标签下封装option标签

    项目中经常用到Select标签,用封装好的方法获取option,可以避免冗赘的代码: 1.JSP--标签 <select class="width_md" name=&quo ...

  10. stark组件之过滤操作【模仿Django的admin】

    一.先看下django的admin是如何实现过滤操作 首先在配置类中顶一个list_filter的列表,把要过滤的字段作为元素写i进去就可以了 class testbook(admin.ModelAd ...