博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 446. Arithmetic Slices II - Subsequence
阅读量:4621 次
发布时间:2019-06-09

本文共 2598 字,大约阅读时间需要 8 分钟。

原题链接在这里:

题目:

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, 97, 7, 7, 73, -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: 7Explanation: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: .

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:

1 class Solution { 2     public int numberOfArithmeticSlices(int[] A) { 3         int n = A.length; 4         Map
[] arrOfMap = new Map[n]; 5 int res = 0; 6 7 for(int i = 0; i
(); 9 10 for(int j = 0; j
Integer.MAX_VALUE || diffLong < Integer.MIN_VALUE){13 continue;14 }15 16 int diff = (int)diffLong;17 int count = arrOfMap[j].getOrDefault(diff, 0);18 res += count;19 20 int original = arrOfMap[i].getOrDefault(diff, 0);21 arrOfMap[i].put(diff, original+count+1);22 }23 }24 25 return res;26 }27 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11441949.html

你可能感兴趣的文章
MySql【Error笔记】
查看>>
vue入门
查看>>
JS线程Web worker
查看>>
Flex的动画效果与变换!(三)(完)
查看>>
mysql常见错误码
查看>>
Openresty 与 Tengine
查看>>
使用XV-11激光雷达做hector_slam
查看>>
布局技巧4:使用ViewStub
查看>>
学习记事
查看>>
java 子类重写父类的方法应注意的问题
查看>>
[LevelDB] LevelDB理论基础
查看>>
【codecombat】 试玩全攻略 第一关kithguard地牢
查看>>
【DP】 POJ 1191 棋盘分割 记忆化搜索
查看>>
自动化测试 Appium之Python运行环境搭建 Part2
查看>>
说说DBA职责和目标
查看>>
从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
查看>>
sql server 实现多表连接查询
查看>>
Python标准库:内置函数getattr(object, name[, default])
查看>>
转:android 自定义RadioButton样式
查看>>
HTTP请求过程
查看>>