MJay

276 - DP - (문제 잘 읽어야하는 이유) 본문

Programming/LeetCode

276 - DP - (문제 잘 읽어야하는 이유)

MJSon 2019. 10. 22. 08:02

No more than 2 edges -> 2개는 같은 색깔이 될수있다는 뜻이였네

 

There is a fence with n posts, each post can be painted with one of the k colors.

You have to paint all the posts such that no more than two adjacent fence posts have the same color.

Return the total number of ways you can paint the fence.

Note:
n and k are non-negative integers.

Example:

Input: n = 3, k = 2 Output: 6 Explanation: Take c1 as color 1, c2 as color 2. All possible ways are:   post1 post2 post3 ----- ----- ----- ----- 1 c1 c1 c2   2 c1 c2 c1   3 c1 c2 c2   4 c2 c1 c1  5 c2 c1 c2   6 c2 c2 c1

 

 

 

재밌는 문제다

 

총 경우의 수

 

1. 같을 경우

 

전 단계가 Different ( 2개가 색이 달라야한다. ), 그 경우에 * 1 -> 같은 색깔 따라다니까

 

same = prev 

 

2. 다를 경우

 

전 단계가 같거나, 다를 경우에 (k-1) 를 곱하면 된다. 

 

이 동영상도 참고했다.

 

https://www.youtube.com/watch?v=deh7UpSRaEY

'Programming > LeetCode' 카테고리의 다른 글

750  (0) 2019.10.26
338 Bits  (0) 2019.10.22
198 - DP - House Robber  (0) 2019.10.21
70 - DP - Climbing Stairs  (0) 2019.10.21
256 - DP - Paint House  (0) 2019.10.21