Skip to content

Instantly share code, notes, and snippets.

@devanshdalal
Created March 13, 2017 14:39
Show Gist options
  • Save devanshdalal/ecc4adff5810920f31b2eebe767e4320 to your computer and use it in GitHub Desktop.
Save devanshdalal/ecc4adff5810920f31b2eebe767e4320 to your computer and use it in GitHub Desktop.
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