Created
March 13, 2017 14:39
-
-
Save devanshdalal/ecc4adff5810920f31b2eebe767e4320 to your computer and use it in GitHub Desktop.
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
int countBinarySequences(int n, int k) { | |
/* | |
* vector<int> dp (short for dynamic programming) | |
* is used for storing the interim values. | |
*/ | |
std::vector<int> dp(k, 0); | |
dp[0] = 1; | |
for (int i = 0; i <= n; i++) { | |
int sum = dp[0]; | |
for (int j = k - 1; j > 0; j--) { | |
sum += dp[j]; | |
dp[j] = dp[j+1] ; | |
} | |
dp[0] = sum; | |
} | |
return dp[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment