Leetcode: 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]
参考了https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution
这道题DP思路还是能想出来,Time O(N^2), Space O(N^2)
T(i, d)
, which denotes the total number of arithmetic subsequence slices ending at index i
with difference d
. The base case and recurrence relation are as follows:
- Base case:
T(0, d) = 0
(This is true for anyd
). - Recurrence relation:
T(i, d) = summation of (1 + T(j, d))
as long as0 <= j < i && d == A[i] - A[j]
.
这个地方有个Corner case, [2,2,3,4,5], 到3的时候,前面有两个2,这个+1具体应该怎么处理,如果直接+1并且用T(i,d)表示total number of arithmetic subsequence slices ending at index i
with difference d的话, 那么到3这个数的时候T(2,1)==2,那不岂是表示3这里有两个valid arithmetic subsequence? 而我们知道其实是0个。
所以我们稍作改变T(i,d)表示total number of “generalized” arithmetic subsequence slices ending at index i
with difference d, 这个"generalized" slices允许长度为2,
比如上例[2,2,3,4,5], 考虑diff==1的情况,到3的时候generalized slices number是2, 分别是[2, 3], [2, 3]
到4的时候generalized slices number是3,分别是[2,3,4], [2,3,4], [3,4]
到5的时候generalized slices number是4, 分别是[2,3,4,5], [2,3,4,5], [3,4,5], [4,5]
如此错了一位,算result的时候也错一位算
这道题又是一道用HashMap来做DP的题,是因为diff大小不确定,没有range,像这种没有range的DP,用HashMap吧
另外语法注意第3行,等号后面map不能再有泛型;第9行等号后面一定要有long
public int numberOfArithmeticSlices(int[] A) {
int res = 0;
Map<Integer, Integer>[] map = new Map[A.length]; for (int i = 0; i < A.length; i++) {
map[i] = new HashMap<>(i); for (int j = 0; j < i; j++) {
long diff = (long)A[i] - A[j];
if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) continue; int d = (int)diff;
int c1 = map[i].getOrDefault(d, 0); //orignial value of T(i, d)
int c2 = map[j].getOrDefault(d, 0); //the counts from T(j, d)
res += c2;
map[i].put(d, c1 + c2 + 1);
}
} return res;
}
Leetcode: Arithmetic Slices II - Subsequence的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- Arithmetic Slices II - Subsequence LT446
446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
- 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)
Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...
- [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 ...
- 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 { ...
- [LeetCode] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
随机推荐
- locale的设定及其LANG、LC_ALL、LANGUAGE环境变量的区别
locale这个单词中文翻译成地区或者地域,其实这个单词包含的意义要宽泛很多.Locale是根据计算机用户所使用的语言,所在国家或者地区,以及当地的文化传统所定义的一个软件运行时的语言环境. [ora ...
- iOS 常用英语翻译
1..serve advertisements within the app 服务应用中的广告.如果你的应用中集成了广告的时候,你需要勾选这一项. √2.Attribute this app in ...
- C#读取数据库字节流生成图片
前台用DataList绑定 <asp:DataList ID="DataList1" runat="server"> <ItemTemplat ...
- 最大乘积 Maximun Product
最大乘积 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 题意: 输入n个元素组成的序列s,你需要 ...
- 序列化,反序列化,模拟ATM机
package com.bank.unionpay; //银行卡的接口 public interface I_yinhangka { //抽象方法 //public abstract默认修饰抽象的 p ...
- mysql主从复制过滤
主从复制过滤: 配置文件中的[mysqld]块中: master:(考虑到即时点还原一般不在主过滤) binlog_do_db= #数据库白名单 binlog_ignore_db= #数据库白名单 s ...
- Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- TestStand与LabVIEW UI 交互
交互起因 客户觉得TestStand界面复杂,希望一个简单的界面即可进行序列执行,采用LabVIEW调用TestStand引擎可实现快速设计,将TestStand拆解到LabVIEW.然而,这样做需要 ...
- sql语句左右表连接理解
一句话,左连接where只影响坐标,右连接where只影响右表
- hdu 4897 Little Devil I
传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4897 题目大意:一棵树,三个操作:1.将某条链取反,2.将与某条链相邻的边取反,3.查询某条链上为1的边 ...