Skip to content

Commit dd4d753

Browse files
authored
Add files via upload
0 parents  commit dd4d753

11 files changed

+29805
-0
lines changed

1.fibonacci_iterative.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<iostream>
2+
using namespace std;
3+
typedef long long ll;
4+
void printFibonacciNumbers(int n){
5+
if(n>=0)cout<<0<<" ";
6+
if(n>=1)cout<<1<<" ";
7+
int prev2=0,prev1=1;
8+
for(int i=2;i<n;i++){
9+
int curr=prev2+prev1;
10+
prev2=prev1;
11+
prev1=curr;
12+
cout<<curr<<" ";
13+
}
14+
return;
15+
}
16+
int main(){
17+
int n;
18+
cout<<"Enter Number"<<endl;
19+
cin>>n;
20+
printFibonacciNumbers(n);
21+
return 0;
22+
23+
}

1.fibonacci_recursive.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<include>
2+
using namespace std;
3+
typedef long long ll;
4+
int func(int n,vector<int>&dp){
5+
if(dp[n]!=-1)return dp[n];
6+
if(n==0 || n==1)return n;
7+
int num=func(n-1,dp)+func(n-2,dp);
8+
return dp[n]=num;
9+
}
10+
int main(){
11+
int n;
12+
cout<<"Enter Number"<<endl;
13+
cin>>n;
14+
vector<int>dp(n+1,-1);
15+
for(int i=0;i<n;i++){
16+
int num=func(i,dp);
17+
cout<<num<<" ";
18+
}
19+
return 0;
20+
}

2.huffmanCoding.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
5+
class Node{
6+
public:
7+
char data;
8+
int freq;
9+
Node *left,*right;
10+
Node(char data,int freq){
11+
this->data=data;
12+
this->freq=freq;
13+
left=right=NULL;
14+
}
15+
};
16+
17+
class cmp{
18+
public:
19+
bool operator()(Node* a,Node* b){
20+
return a->freq>b->freq;
21+
}
22+
};
23+
24+
void printTree(Node *head,string str=""){
25+
if(head==NULL)return;
26+
if(head->left==NULL && head->right==NULL){
27+
cout<<head->data<<" -> "<<str<<endl;
28+
return;
29+
}
30+
printTree(head->left,str+"0");
31+
printTree(head->right,str+"1");
32+
}
33+
34+
Node* buildTree(vector<char>&arr,vector<int>&frequency){
35+
priority_queue<Node*,vector<Node*>,cmp>pq;
36+
for(int i=0;i<arr.size();i++){
37+
pq.push(new Node(arr[i],frequency[i]));
38+
}
39+
while(pq.size()>1){
40+
Node *a=pq.top();
41+
pq.pop();
42+
Node *b=pq.top();
43+
pq.pop();
44+
Node *c=new Node('$',a->freq+b->freq);
45+
c->left=a;
46+
c->right=b;
47+
pq.push(c);
48+
}
49+
return pq.top();
50+
}
51+
52+
int main(){
53+
int n;
54+
cin>>n;
55+
vector<char>arr(n);
56+
vector<int>frequency(n);
57+
for(int i=0;i<n;i++){
58+
cin>>arr[i];
59+
}
60+
for(int i=0;i<n;i++){
61+
cin>>frequency[i];
62+
}
63+
Node *head=buildTree(arr,frequency);
64+
printTree(head);
65+
return 0;
66+
}

3.fractionalKnapsack.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
class Item{
5+
public:
6+
int value;
7+
int weight;
8+
};
9+
10+
bool cmp(Item a,Item b){
11+
double r1=(double)a.value/(double)a.weight;
12+
double r2=(double)b.value/(double)b.weight;
13+
return r1>r2;
14+
}
15+
16+
double fractionalKnapsack(vector<Item>items,int W){
17+
sort(items.begin(),items.end(),cmp);
18+
double mx=0;
19+
for(int i=0;i<items.size();i++){
20+
if(items[i].weight<=W){
21+
mx+=items[i].value;
22+
W-=items[i].weight;
23+
}
24+
else{
25+
mx+=(items[i].value/(double)items[i].weight)*(double)W;
26+
break;
27+
}
28+
}
29+
return mx;
30+
}
31+
int main(){
32+
int n,W;
33+
cin>>n>>W;
34+
vector<Item>items(n);
35+
for(int i=0;i<n;i++){
36+
cin>>items[i].value;
37+
cin>>items[i].weight;
38+
}
39+
double maxValue=fractionalKnapsack(items,W);
40+
cout<<maxValue<<endl;
41+
return 0;
42+
}

4.0_1Knapsack.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
int func(int i,int j,vector<int> &val,vector<int> &wt,vector<vector<int>> &dp){
5+
if(i==0){
6+
if(wt[i]<=j)return val[i];
7+
else return 0;
8+
}
9+
if(dp[i][j]!=-1)return dp[i][j];
10+
int notTake=func(i-1,j,val,wt,dp);
11+
int take=0;
12+
if(wt[i]<=j){
13+
take=val[i]+func(i-1,j-wt[i],val,wt,dp);
14+
}
15+
return dp[i][j]=max(take,notTake);
16+
}
17+
int knapsack(int n,vector<int>&val,vector<int>&wt,int W){
18+
vector<vector<int>>dp(n,vector<int>(W+1,-1));
19+
int ans=func(n-1,W,val,wt,dp);
20+
21+
return ans;
22+
}
23+
24+
// int knapsack(int n,vector<int>&val,vector<int>&wt,int W){
25+
// iterative approach
26+
// can be space optimized
27+
// vector<vector<int>>dp(n,vector<int>(W+1,0));
28+
// for(int j=wt[0];j<=W;j++)dp[0][j]=val[0];
29+
// for(int i=1;i<n;i++){
30+
// for(int j=0;j<=W;j++){
31+
// int notTake=dp[i-1][j];
32+
// int take=0;
33+
// if(wt[i]<=j){
34+
// take=val[i]+dp[i-1][j-wt[i]];
35+
// }
36+
// dp[i][j]=max(take,notTake);
37+
// }
38+
// }
39+
// return dp[n-1][W];
40+
// }
41+
int main(){
42+
int n,W;
43+
cin>>n>>W;
44+
vector<int>val(n),wt(n);
45+
for(int i=0;i<n;i++){
46+
cin>>val[i];
47+
}
48+
for(int i=0;i<n;i++){
49+
cin>>wt[i];
50+
}
51+
int maxValue=knapsack(n,val,wt,W);
52+
cout<<maxValue<<endl;
53+
return 0;
54+
}

5.nQueenProblem.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
bool isValid(int row,int col,vector<vector<char>>&board,int n){
5+
6+
int dup_row=row;
7+
int dup_col=col;
8+
while(col>=0 && row>=0){
9+
if(board[row][col]=='Q')return false;
10+
row--;
11+
col--;
12+
}
13+
row=dup_row;
14+
col=dup_col;
15+
while(col>=0){
16+
if(board[row][col]=='Q')return false;
17+
col--;
18+
}
19+
col=dup_col;
20+
while(row<n && col>=0){
21+
if(board[row][col]=='Q')return false;
22+
row++;
23+
col--;
24+
}
25+
return true;
26+
}
27+
bool placeQueen(int col,vector<vector<char>>&board,int n){
28+
if(col==n)return true;
29+
for(int i=0;i<n;i++){
30+
if(isValid(i,col,board,n)){
31+
board[i][col]='Q';
32+
bool check=placeQueen(col+1,board,n);
33+
if(check==true)return true;
34+
board[i][col]='.';
35+
}
36+
}
37+
return false;
38+
}
39+
int main(){
40+
int n;
41+
cin>>n;
42+
vector<vector<char>>board(n,vector<char>(n));
43+
for(int i=0;i<n;i++){
44+
for(int j=0;j<n;j++){
45+
board[i][j]='.';
46+
}
47+
}
48+
placeQueen(0,board,n);
49+
for(int i=0;i<n;i++){
50+
for(int j=0;j<n;j++){
51+
cout<<board[i][j]<<" ";
52+
}
53+
cout<<endl;
54+
}
55+
return 0;
56+
}

0 commit comments

Comments
 (0)