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. json数组转java对象

    <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>ison-lib</art ...

  2. canvas 动画 时钟clock

    <canvas id="clock" width="500" height="500"></canvas> func ...

  3. shell重定向命令执行顺序

    重定向内容介绍 一条shell命令的执行包含三个文件描述符:标准输入(键盘等) stdin 0,标准正确输出(屏幕等) stdout 1,标准错误输出(屏幕等)stderr 2   通过重定向可以指定 ...

  4. python--第三天总结

    [collection系列]1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 c = Counter('abcdeabc ...

  5. 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确

    安装好后,登录后台提示 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确 检查mysql是否启动netstat -lnpt是否有3306端口? 一 有A 检查/www/wdlinu ...

  6. 牛客练习赛43-F(简单容斥)

    题目链接:https://ac.nowcoder.com/acm/contest/548/F 题意:简化题意之后就是求[1,n]中不能被[2,m]中的数整除的数的个数. 思路:简单容斥题,求[1,n] ...

  7. 第四章 栈与队列(c2)栈应用:括号匹配

  8. python字典设置初始值setdefault()与get()

    L = ['you','me','you','me','you','me','you'] D = {} for i in L: D[i] += 1 print(D) 执行以下代码会发生错误 Trace ...

  9. 【转】收集 jetty、tomcat、jboss、weblogic 的比较

    jetty Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开 ...

  10. http://218.245.4.98:20000/phpmyadmin:2018SCTF--easiest web - phpmyadmin

      SCTF的web最简单题,好难好难好难.      直接进去就是PHPmyadmin界面(即mysql的网页界面),需要登录密码,这个我当时没有破解出来,谁知道账号密码是root/root咩,要是 ...