Skip to content

Commit b08ffc8

Browse files
committed
day 18
1 parent a55e1e6 commit b08ffc8

File tree

3 files changed

+92
-20
lines changed

3 files changed

+92
-20
lines changed

DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
- [Count and Say](https://leetcode.com/problems/count-and-say/ "view question") - [Cpp Solution](./solutions/Count%20and%20Say.cpp)
6565
- [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/ "view question") - [Cpp Solution](./solutions/Longest%20Palindromic%20Substring.cpp)
6666
- []( "view question") - [Cpp Solution](./solutions/.cpp)
67+
- []( "view question") - [Cpp Solution](./solutions/.cpp)
6768

6869
### Searching & Sorting
6970

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Arithmetic Slices
3+
=================
4+
5+
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.
6+
7+
For example, these are arithmetic sequences:
8+
9+
1, 3, 5, 7, 9
10+
7, 7, 7, 7
11+
3, -1, -5, -9
12+
The following sequence is not arithmetic.
13+
14+
1, 1, 2, 5, 7
15+
16+
A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.
17+
18+
A slice (P, Q) of the array A is called arithmetic if the sequence:
19+
A[P], A[P + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.
20+
21+
The function should return the number of arithmetic slices in the array A.
22+
23+
Example:
24+
A = [1, 2, 3, 4]
25+
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
26+
*/
27+
28+
class Solution
29+
{
30+
public:
31+
int nThSum(int n)
32+
{
33+
return n * (n + 1) / 2;
34+
}
35+
36+
int numberOfArithmeticSlices(vector<int> &A)
37+
{
38+
if (A.size() <= 2)
39+
return 0;
40+
41+
int ans = 0, count = 0;
42+
int a = 0, b = 1, diff = A[1] - A[0];
43+
44+
while (b < A.size())
45+
{
46+
if (A[b] - A[a] == diff)
47+
{
48+
count++;
49+
a++;
50+
b++;
51+
}
52+
else
53+
{
54+
if (count > 1)
55+
{
56+
int nums = count - 1;
57+
ans += nThSum(nums);
58+
}
59+
count = 0;
60+
diff = A[b] - A[a];
61+
}
62+
}
63+
64+
if (count > 1)
65+
{
66+
int nums = count - 1;
67+
ans += nThSum(nums);
68+
}
69+
70+
return ans;
71+
}
72+
};
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
# February 2020 LeetCoding Challenge
22

3-
| Day | Question Links | Solutions |
4-
| :-: | :------------- | :-------: |
5-
| 1. | [Number of 1 Bits](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3625/) | [cpp](./01.%20Number%20of%201%20Bits.cpp) |
6-
| 2. | [Trim a Binary Search Tree](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3626/) | [cpp](./02.%20Trim%20a%20Binary%20Search%20Tree.cpp) |
7-
| 3. | [Linked List Cycle](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3627/) | [cpp](./03.%20Linked%20List%20Cycle.cpp) |
8-
| 4. | [Longest Harmonious Subsequence](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3628/) | [cpp](./04.%20Longest%20Harmonious%20Subsequence.cpp) |
9-
| 5. | [Simplify Path](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3629/) | [cpp](./05.%20Simplify%20Path.cpp) |
10-
| 6. | [Binary Tree Right Side View](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3630/) | [cpp](./06.%20Binary%20Tree%20Right%20Side%20View.cpp) |
11-
| 7. | [Shortest Distance to a Character](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3631/) | [cpp](./07.%20Shortest%20Distance%20to%20a%20Character.cpp) |
12-
| 8. | [Peeking Iterator](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3633/) | [cpp](./08.%20Peeking%20Iterator.cpp) |
13-
| 9. | [Convert BST to Greater Tree](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3634/) | [cpp](./09.%20Convert%20BST%20to%20Greater%20Tree.cpp) |
14-
| 10. | [Copy List with Random Pointer](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3635/) | [cpp](./10.%20Copy%20List%20with%20Random%20Pointer.cpp) |
15-
| 11. | [Valid Anagram](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3636/) | [cpp](./11.%20Valid%20Anagram.cpp) |
16-
| 12. | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3637/) | [cpp](./12.%20Number%20of%20Steps%20to%20Reduce%20a%20Number%20to%20Zero.cpp) |
17-
| 13. | [Shortest Path in Binary Matrix](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3638/) | [cpp](./13.%20Shortest%20Path%20in%20Binary%20Matrix.cpp) |
18-
| 14. | [Is Graph Bipartite?](https://leetcode.com/explore/featured/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3639/) | [cpp](./14.%20Is%20Graph%20Bipartite.cpp) |
19-
| 15. | [The K Weakest Rows in a Matrix](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/586/week-3-february-15th-february-21st/3641/) | [cpp](./15.%20The%20K%20Weakest%20Rows%20in%20a%20Matrix.cpp) |
20-
| 16. | [Letter Case Permutation](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/586/week-3-february-15th-february-21st/3642/) | [cpp](./16.%20Letter%20Case%20Permutation.cpp) |
21-
22-
3+
| Day | Question Links | Solutions |
4+
| :-: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------: |
5+
| 1. | [Number of 1 Bits](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3625/) | [cpp](./01.%20Number%20of%201%20Bits.cpp) |
6+
| 2. | [Trim a Binary Search Tree](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3626/) | [cpp](./02.%20Trim%20a%20Binary%20Search%20Tree.cpp) |
7+
| 3. | [Linked List Cycle](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3627/) | [cpp](./03.%20Linked%20List%20Cycle.cpp) |
8+
| 4. | [Longest Harmonious Subsequence](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3628/) | [cpp](./04.%20Longest%20Harmonious%20Subsequence.cpp) |
9+
| 5. | [Simplify Path](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3629/) | [cpp](./05.%20Simplify%20Path.cpp) |
10+
| 6. | [Binary Tree Right Side View](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3630/) | [cpp](./06.%20Binary%20Tree%20Right%20Side%20View.cpp) |
11+
| 7. | [Shortest Distance to a Character](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3631/) | [cpp](./07.%20Shortest%20Distance%20to%20a%20Character.cpp) |
12+
| 8. | [Peeking Iterator](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3633/) | [cpp](./08.%20Peeking%20Iterator.cpp) |
13+
| 9. | [Convert BST to Greater Tree](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3634/) | [cpp](./09.%20Convert%20BST%20to%20Greater%20Tree.cpp) |
14+
| 10. | [Copy List with Random Pointer](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3635/) | [cpp](./10.%20Copy%20List%20with%20Random%20Pointer.cpp) |
15+
| 11. | [Valid Anagram](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3636/) | [cpp](./11.%20Valid%20Anagram.cpp) |
16+
| 12. | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3637/) | [cpp](./12.%20Number%20of%20Steps%20to%20Reduce%20a%20Number%20to%20Zero.cpp) |
17+
| 13. | [Shortest Path in Binary Matrix](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3638/) | [cpp](./13.%20Shortest%20Path%20in%20Binary%20Matrix.cpp) |
18+
| 14. | [Is Graph Bipartite?](https://leetcode.com/explore/featured/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3639/) | [cpp](./14.%20Is%20Graph%20Bipartite.cpp) |
19+
| 15. | [The K Weakest Rows in a Matrix](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/586/week-3-february-15th-february-21st/3641/) | [cpp](./15.%20The%20K%20Weakest%20Rows%20in%20a%20Matrix.cpp) |
20+
| 16. | [Letter Case Permutation](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/586/week-3-february-15th-february-21st/3642/) | [cpp](./16.%20Letter%20Case%20Permutation.cpp) |
21+
| 18. | [Arithmetic Slices](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/586/week-3-february-15th-february-21st/3644/) | [cpp](./18.%20Arithmetic%20Slices.cpp) |

0 commit comments

Comments
 (0)