Skip to content

Commit f490f71

Browse files
committed
Dec18
1 parent 8e81129 commit f490f71

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Hints for ~250 leetcode problems. Mostly medium, some hard.
44
Good practice list for internships/job hunt.
55

6-
If this helps you, please star the repo (I commit a lot, so I suggest you unwatch this repo).
6+
If this helps you, please star the repo (I commit a lot, so I suggest you unwatch the repo).
77

88
Solutions <a href = "https://github.com/aliasvishnu/leetcode/tree/master/Solutions" > here </a>
99

@@ -79,6 +79,7 @@ Solutions <a href = "https://github.com/aliasvishnu/leetcode/tree/master/Solutio
7979
| 118 | BF | Generate new row from old row. | O(n^2) |
8080
| 119 | BF | Generate new row from old row. | O(n^2) |
8181
| 128 | DP | DP problem, find recurrence relation. | |
82+
| 413 | Array | [1, 2, 3, 4, 5, 7, 9, 11]. can be written as [5, 3] i.e. 5 sequence of difference 1 and 3 sequence of difference 2, you need to figure out how many parts you can split 5 and 3 into. | O(n) |
8283

8384
## Second hint list
8485

Solutions/413-ArithmeticSlices.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
4+
// n = number of values sequences are different for, 1, 2, 3, 4, 5 becomes 1, 1, 1, 1 i.e. n = 4.
5+
int f(int n){
6+
return (n*n+n)/2;
7+
}
8+
9+
int numberOfArithmeticSlices(vector<int>& A) {
10+
int len = A.size();
11+
if(len <= 2) return 0;
12+
13+
14+
int curdiff = A[1] - A[0], curcount = 0, answer = 0;
15+
16+
for(int i = 2; i < len; i++){
17+
if(A[i]-A[i-1] == curdiff) curcount += 1;
18+
else{
19+
answer += f(curcount);
20+
curcount = 0;
21+
curdiff = A[i]-A[i-1];
22+
}
23+
24+
}
25+
26+
answer += f(curcount);
27+
28+
29+
return answer;
30+
}
31+
};

0 commit comments

Comments
 (0)