原题链接在这里: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的更多相关文章

  1. 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)

    Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...

  2. 446. Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  3. 446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { ...

  4. Arithmetic Slices II - Subsequence LT446

    446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...

  5. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  6. Leetcode: Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  7. [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 ...

  8. LeetCode446. Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  9. LeetCode 413 Arithmetic Slices详解

    这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...

随机推荐

  1. 英语insuraunce保险

    中文名:保险 外文名:insurance或insuraunce 类型:保障机制,商业行为 作用:资金融通.损失补偿等 原则:分摊.代位.大数法则等原则 性质:契约经济关系 意义:市场经济条件下风险管理 ...

  2. Oracle数据库 常用的触发器命令

    创建自增序列,创建触发器(在触发时间中操纵序列,实现主键自增): Oracle数据库不支持自增方法 create sequence seq_userInfo_usid start with ;--创建 ...

  3. C#各种字段类型对比

    一.常量.只读字段.静态字段和静态只读字段对比 public class ModelClass { //常量在定义时必须赋初始值 //public const string constField; p ...

  4. Android App 架构演变

    文:https://www.jianshu.com/p/ce26e7960926 最近App项目(MVC架构)越做越大,协同开发效率较低,维护困难,所以产生了调整架构的想法,在 简书.csdn.知乎上 ...

  5. 实战FFmpeg + OpenGLES--iOS平台上视频解码和播放

    一个星期的努力终于搞定了视频的播放,利用FFmpeg解码视频,将解码的数据通过OpenGLES渲染播放.搞清楚了自己想知道的和完成了自己的学习计划,有点小兴奋.明天就是“五一”,放假三天,更开心啦. ...

  6. SAP Hybris Discount group,折扣组,折扣记录,用户组几组概念的关联

    在backoffice的Price Settings->Discount->Customer Discount Groups菜单下面,创建一个新的Customer Discount Gro ...

  7. Java里面获取当前服务器(linux环境)的IP地址--与请求者的真实IP

    package com.wfd360.Util; import javax.servlet.http.HttpServletRequest; import java.net.Inet4Address; ...

  8. mysql 设置查看字符集

    MySQL查看和修改字符集的方法   一.查看字符集 1.查看MYSQL数据库服务器和数据库字符集 方法一:show variables like '%character%';方法二:show var ...

  9. iManager微服务自定义上传数据的两种方法

    背景 当数据量大时,通过浏览器上传可能速度较慢,可以使用以下两种方式进行数据导入. 方法一 1.访问k8s主页(31234端口) 找到所创建的微服务的命名空间(例如icloud-native-xxx) ...

  10. UML——从类图到C++

    简易软件开发流程 实践中,use case and description.class diagram与sequence diagram三者搭配,几乎是UML项目的基本类型,所以在分工或外包的设计文档 ...