forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArthimeticSlicing.java
More file actions
34 lines (32 loc) · 826 Bytes
/
ArthimeticSlicing.java
File metadata and controls
34 lines (32 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
// TC : O(n)
// SC : O(n)
public int numberOfArithmeticSlices(int[] A) {
int[] dp = new int[A.length];
int total = 0;
for(int i=2;i<A.length;i++){
if(A[i] - A[i-1] == A[i-1] - A[i-2]){
dp[i] = dp[i-1]+1;
} else{
dp[i] = 0;
}
total += dp[i];
}
return total;
}
TC: O(n)
SC: O(1)
public int numberOfArithmeticSlices(int[] A) {
int previousCount = 0;
int total = 0;
for(int i=2;i<A.length;i++){
if(A[i] - A[i-1] == A[i-1] - A[i-2]){
previousCount = previousCount +1;
} else{
previousCount = 0;
}
total += previousCount;
}
return total;
}
}