File tree 2 files changed +56
-0
lines changed
Leetcode Daily Challenge/August-2021
2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Paint Fence
3
+ ===========
4
+
5
+ You are painting a fence of n posts with k different colors. You must paint the posts following these rules:
6
+
7
+ Every post must be painted exactly one color.
8
+ There cannot be three or more consecutive posts with the same color.
9
+ Given the two integers n and k, return the number of ways you can paint the fence.
10
+
11
+ Example 1:
12
+ Input: n = 3, k = 2
13
+ Output: 6
14
+ Explanation: All the possibilities are shown.
15
+ Note that painting all the posts red or all the posts green is invalid because there cannot be three posts in a row with the same color.
16
+
17
+ Example 2:
18
+ Input: n = 1, k = 1
19
+ Output: 1
20
+
21
+ Example 3:
22
+ Input: n = 7, k = 2
23
+ Output: 42
24
+
25
+ Constraints:
26
+ 1 <= n <= 50
27
+ 1 <= k <= 105
28
+ The testcases are generated such that the answer is in the range [0, 231 - 1] for the given n and k.
29
+ */
30
+
31
+ class Solution
32
+ {
33
+ public:
34
+ int numWays (int n, int k)
35
+ {
36
+ if (n == 1 )
37
+ return k;
38
+ if (n == 2 )
39
+ return k * k;
40
+
41
+ int prevTwo = k;
42
+ int prevOne = k * k;
43
+
44
+ int ans = prevTwo;
45
+
46
+ for (int i = 3 ; i <= n; ++i)
47
+ {
48
+ ans = (k - 1 ) * prevOne + (k - 1 ) * prevTwo;
49
+ prevTwo = prevOne;
50
+ prevOne = ans;
51
+ }
52
+
53
+ return ans;
54
+ }
55
+ };
Original file line number Diff line number Diff line change 10
10
| 5. | [ Stone Game] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3870/ ) | [ cpp] ( ./05.%20Stone%20Game.cpp ) |
11
11
| 6. | [ N-ary Tree Level Order Traversal] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3871/ ) | [ cpp] ( ./06.%20N-ary%20Tree%20Level%20Order%20Traversal.cpp ) |
12
12
| 7. | [ Palindrome Partitioning II] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3872/ ) | [ cpp] ( ./07.%20Palindrome%20Partitioning%20II.cpp ) |
13
+ | | [ Paint Fence] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3873/ ) | [ cpp] ( ./Paint%20Fence.cpp ) |
13
14
| 9. | [ Add Strings] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3875/ ) | [ cpp] ( ./09.%20Add%20Strings.cpp ) |
14
15
| 10. | [ Flip String to Monotone Increasing] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3876/ ) | [ cpp] ( ./10.%20Flip%20String%20to%20Monotone%20Increasing.cpp ) |
15
16
| 11. | [ Array of Doubled Pairs] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3877/ ) | [ cpp] ( ./11.%20Array%20of%20Doubled%20Pairs.cpp ) |
You can’t perform that action at this time.
0 commit comments