Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Dynamic Programming: Pascal's Triangle #125

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Data Structures/T14-Dynamic Programming/6.1 Pascal's Triangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// C++ program to
// to print Pascal's Triangle till nth row.

#include<iostream>
#include<vector>

using namespace std;

vector<vector<int> > PascalTriangle(int numRows);

int main()
{
int n;
cout << "Enter the number of rows to print in Pascal's Triangle: ";
cin >> n;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
cout << PascalTriangle(n)[i][j] << " ";
}
cout << endl;
}
}

vector<vector<int> > PascalTriangle(int numRows)
{
vector<vector<int>> pascal;
for(int i=0;i<numRows;i++)
{
vector<int> row(i+1,1);
pascal.push_back(row);
for(int j=1;j<i;j++)
{
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
}
}
return pascal;
}


/* Test Case 1

Enter the number of rows to print in Pascal's Triangle: 6

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Test Case 2

Enter the number of rows to print in Pascal's Triangle: 10

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Link to the problem: https://leetcode.com/problems/pascals-triangle/

*/
43 changes: 43 additions & 0 deletions LeetCode/top 100 Liked Question/ClimbingStairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<iostream>

using namespace std;

int ClimbStairs(int n);

int main()
{
cout << "Enter the number of stairs: ";
int n;
cin >> n;
cout << ClimbStairs(n) << endl;
}

int ClimbStairs(int n)
{
int dp[n+1];
dp[0] = 1; // If there the stairs are 0 and 1 in number, then the possible ways to climb are 1 and 1 respectively.
dp[1] = 1;
for(int i=2;i<=n;i++)
{
dp[i] = dp[i-1] + dp[i-2]; // To climb to nth stairs, the number of ways depends on the number of ways to climb (n-1)th and (n-2)th stairs
}
return dp[n];
}

/*

*** Test Case 1 ***

Enter the number of stairs: 6

13

*** Test Case 2 ***

Enter the number of stairs: 56

363076002

Link to the problem: https://leetcode.com/problems/climbing-stairs/

*/