Arithmetic Slices LT413
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的个数,那么
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 = (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的更多相关文章
- Arithmetic Slices II - Subsequence LT446
446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- [LeetCode]413 Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.*********************** ...
- 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 ...
- Leetcode: Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- [Swift]LeetCode413. 等差数列划分 | Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- LeetCode——Arithmetic Slices
Question A sequence of number is called arithmetic if it consists of at least three elements and if ...
随机推荐
- Java中String的常用方法总结
Java中String的常用方法总结 1.length()字符串的长度 String str="HelloWord"; System.out.println(str.length( ...
- linux suse 同步时间
ntpdate 210.72.145.44 ip为中国(国家授时中心)
- 算法之LOWB三人组之选择排序
选择排序 思想是在一个列表中每次循环一遍,拿到最小值,接着再从剩下的无序区中继续拿最小值,如此循环,直到结束. 时间复杂度为O(n^2) # 最简单的一个选择排序,循环一个列表,拿到最小值,添加到一个 ...
- 【Django】关于scss 的安装
今天看视频教程的时候发现老师的样式文件改用了scss(然鹅我买的1块钱特价课程其实是节选出来的,所以前面没有看到过关于scss的介绍) 然后我本以为把原来的css改名字为scss就行,然鹅没有效果. ...
- pycharm的安装(图文)
pycharm的安装, PyCharm是一种 IDE,可以在里面对python代码调试.语法高亮.Project管理.跳转.智能提示.自动完成.单元测试.版本控制.pycharm提供了一些高级功能,以 ...
- poj1308(并查集)
题目链接:http://poj.org/problem;jsessionid=436A34AE4BE856FB2DF9B264DCA9AA4E?id=1308 题意:给定一些边让你判断是否构成数. 思 ...
- random 模块 时间模块(time) sys模块 os模块
random 模块 1.随机小数 random.random() 0-1内的随机小数 random.uniform(1,5) 1-5范围内的随机小数 2.随机整数 random.randint( ...
- Unity2017五子棋大战_人机_双人_UNET联网
五子棋大战源码工程基于Unity2017.2进行开发,分为人机.双人.UNET网络三种对战方式,配有案例讲解视频, 其中人机五子棋AI有三种开发难度,欢迎有兴趣的同学加入学习! . 目录 000-展示 ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- 使用命令行执行sql文件
用Navicat 导入sqlserver数据库时,出现了out of memory异常,百度无果,想起之前用命令行导入过,再次试了一下成功 用法: 打开执行命令: sqlcmd -S localhos ...