LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/
题目:
A sequence of numbers 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 sequences:
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 subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N.
A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.
The function should return the number of arithmetic subsequence slices in the array A.
The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.
Example:
Input: [2, 4, 6, 8, 10] Output: 7 Explanation:
All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
题解:
Following question: Arithmetic Slices.
The difference here is that subsequence doesn't need to be continuous. e.g. [2,6,10].
Thus when iterate to index i, it needs to go through all the j (j<i) from the beginning.
With A[i] and A[j], the diff = A[i] - A[j]. It needs to know the number of sequences(length doesn't need to be more than 2, 2 is okay) ending at A[j] with the same diff. Accumulate that to result.
Thus here, use an array of map to maintain the difference with frequency for each index.
There could be duplicate numbers in A. Thus at i, same diff may appear, get the original and add the new.
Time Complexity: O(n^2). n = A.length.
Space: O(n^2). Each map could be O(n), there are totally n maps.
AC Java:
class Solution {
public int numberOfArithmeticSlices(int[] A) {
int n = A.length;
Map<Integer, Integer> [] arrOfMap = new Map[n];
int res = 0;
for(int i = 0; i<n; i++){
arrOfMap[i] = new HashMap<>();
for(int j = 0; j<i; j++){
long diffLong = (long)A[i] - A[j];
if(diffLong > Integer.MAX_VALUE || diffLong < Integer.MIN_VALUE){
continue;
}
int diff = (int)diffLong;
int count = arrOfMap[j].getOrDefault(diff, 0);
res += count;
int original = arrOfMap[i].getOrDefault(diff, 0);
arrOfMap[i].put(diff, original+count+1);
}
}
return res;
}
}
LeetCode 446. Arithmetic Slices II - Subsequence的更多相关文章
- 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)
Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...
- 446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- 446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { ...
- 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 II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [Swift]LeetCode446. 等差数列划分 II - 子序列 | Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
随机推荐
- day48——css样式
day48 通过调试窗口还可以玩一个神奇的东西 document.body.contentEditable=true css样式 高度宽度 width宽度 height高度 块级标签能设置高度宽度,内 ...
- 大数据之路【第十篇】:kafka消息系统
一.简介 1.简介 简 介• Kafka是Linkedin于2010年12月份开源的消息系统• 一种分布式的.基于发布/订阅的消息系统 2.特点 – 消息持久化:通过O(1)的磁盘数据结构提供数据的持 ...
- vue中sessionStorage的使用
转载:https://www.cnblogs.com/denken/p/11197612.html localStorage 和 sessionStorage 属性允许在浏览器中存储 key/valu ...
- Rsync学习之旅中
rsync配置文件详解 配置文件内容说明 man rsyncd.conf 全局参数 rsyncd.conf参数 参数说明 uid=rsync 运行rsync守护进程的用户. gid=rsync 运行r ...
- 三伏天里小试牛刀andriod 开发 #华为云·寻找黑马程序员#【华为云技术分享】
2019年07月,北京,三伏天,好热啊.越热自己还越懒得动换(肉身给的信号),但是做为产品经理/交互设计师的,总想着思考些什么(灵魂上给的信号),或者是学习些什么,更有利于将来的职业发展吧,哈哈哈.工 ...
- Visual Studio 2019 使用.Net Core 3.0 二
一.遇到难题 在微软官方逛了一圈,看到了这个. 马上点击,进去看看什么情况. 1.安装previewVisual studio 2019 2.设置SDK previews in Visual Stud ...
- nginx反向代理、缓存及压缩配置实战
一.反向代理配置 (原文链接:http://www.studyshare.cn/blog/details/1155/0 ) 准备:两个项目分别使用端口8080,8081,只有一个备案域名,配置如下 ...
- Java自学-数组 二维数组
Java 如何使用二维数组 这是一个一维数组, 里面的每一个元素,都是一个基本类型int int a[] =new int[]{1,2,3,4,5}; 这是一个二维数组,里面的每一个元素,都是一个一维 ...
- git reset --hard HEAD^后显示more?的原因及如何解决
在windows的cmd控制台下操作git,想要回滚到上一次提交,但是输入git reset --hard HEAD^后就显示more? fatal: ambiguous argument 'HEAD ...
- Iris Network Traffic Analyzer嗅探器
网卡配置 ftp测试