Created
April 11, 2020 12:52
-
-
Save adamkorg/ea152f9e140f5aaaecc7017e9514954f to your computer and use it in GitHub Desktop.
Leetcode 276: Paint Fence (O(n) space)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int numWays(int n, int k) { | |
if (n==0 || k==0) return 0; | |
vector<int> dp(n,1); | |
for (int c=1; c<n; ++c) { | |
int sumPrevCol = dp[c-1]*k; | |
dp[c] = sumPrevCol - (c<2 ? 0 : dp[c-2]); | |
} | |
return dp[n-1]*k; | |
} | |
int main() { | |
cout << numWays(3, 2) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment