Skip to content

Commit 47c6049

Browse files
🎉 Day 18
1 parent a0279a0 commit 47c6049

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
1. [Non-overlapping Intervals](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3425/) ➡️ [CPP Solution](Week3/eraseOverlapIntervals.cpp)
2727
2. [Best Time to Buy and Sell Stock III](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3426/) ➡️ [CPP Solution](Week3/maxProfit.cpp)
2828
3. [Distribute Candies to People](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3427/) ➡️ [CPP Solution](Week3/distributeCandies.cpp)
29+
4. [Numbers With Same Consecutive Differences](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3428/) ➡️ [CPP Solution](Week3/numsSameConsecDiff.cpp)
2930

3031
## Week 4 🚧
3132
Coming Soon...

Week3/numsSameConsecDiff.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
void dfs(int K, int currentNumber, int digitsLeft, vector<int>& res) {
3+
if(digitsLeft == 0) {
4+
res.push_back(currentNumber);
5+
return;
6+
}
7+
8+
int lastDigit = currentNumber % 10;
9+
10+
if((lastDigit + K) <= 9) {
11+
dfs(K, (currentNumber * 10) + lastDigit + K, digitsLeft - 1, res);
12+
}
13+
14+
if((lastDigit - K) >= 0 && K != 0) {
15+
dfs(K, (currentNumber * 10) + lastDigit - K, digitsLeft - 1, res);
16+
}
17+
}
18+
public:
19+
vector<int> numsSameConsecDiff(int N, int K) {
20+
vector<int> res;
21+
22+
if(N == 1) return {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
23+
24+
for(int i = 1; i < 10; ++i) {
25+
dfs(K, i, N - 1, res);
26+
}
27+
28+
return res;
29+
}
30+
};

0 commit comments

Comments
 (0)