Skip to content

Commit 9e3e1ca

Browse files
committed
nothing much
1 parent 60cac9b commit 9e3e1ca

18 files changed

+686
-0
lines changed

catalan.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
4+
using namespace std;
5+
int a[10];
6+
int main()
7+
{
8+
a[0] = 1; a[1] = 1;a[2] = 2;
9+
int n;
10+
cin >>n;
11+
int ans = 0;
12+
for (int i = 0 ; i < n ; i++ ){
13+
ans+=a[i]*a[n-i-1]
14+
}
15+
16+
return 0;
17+
}

coinchangedp.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <climits>
5+
#include <cstring>
6+
7+
using namespace std;
8+
int C[101];
9+
10+
int noofcoins(int P,vector <int>& v)
11+
{
12+
memset(C,INT_MAX,sizeof(C));
13+
C[0]=0;
14+
int n=v.size();
15+
for(int i=1;i<=P;i++)
16+
{
17+
int mn=INT_MAX;
18+
for(int j=0;j<n;j++){
19+
if(v[j]<=i && C[i-v[j]] < mn)
20+
{
21+
mn=C[i-v[j]];
22+
C[i]=mn;
23+
}
24+
}
25+
C[i]+=1;
26+
}
27+
return C[P];
28+
29+
}
30+
int main()
31+
{
32+
vector <int > v;
33+
v.push_back(1);
34+
v.push_back(3);
35+
v.push_back(5);
36+
int ans = noofcoins(11,v);
37+
cout << ans<<endl;
38+
return 0;
39+
}

dijkstra.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <queue>
5+
#include <cstring>
6+
#include <functional>
7+
8+
using namespace std;
9+
10+
typedef pair <int, int> pii;
11+
typedef vector <pii> vpii;
12+
typedef vector <vpii> vvpii;
13+
14+
15+
int main()
16+
{
17+
vvpii graph;
18+
graph.resize(7);
19+
20+
/*Sample Graph*/
21+
22+
graph[1].push_back(pii(10,2)); graph[1].push_back(pii(5,4));
23+
graph[2].push_back(pii(1,3)); graph[2].push_back(pii(2,4));
24+
graph[3].push_back(pii(4,5));
25+
graph[4].push_back(pii(3,2)); graph[4].push_back(pii(9,3)); graph[4].push_back(pii(2,5));
26+
graph[5].push_back(pii(7,1)); graph[5].push_back(pii(3,6));
27+
28+
/*Sample Graph ends*/
29+
30+
int d[6]; int src; int parent[6];
31+
memset(d,1000000,sizeof(d));
32+
cin>>src;
33+
d[src] = 0;parent[src] = NULL;
34+
priority_queue <pii,vpii,greater<pii> > Q;
35+
Q.push(pii(0,1));
36+
while(!Q.empty()){
37+
pii tmp = Q.top();
38+
Q.pop();
39+
int w = tmp.first; int v = tmp.second;
40+
for(int i = 0 ; i < graph[v].size(); i++){
41+
int w2 = graph[v][i].first; int v2 = graph[v][i].second;
42+
if(d[v2] > d[v] + w2){
43+
d[v2] = d[v]+w2;
44+
parent[v2] = v ;
45+
Q.push(pii(d[v2],v2));
46+
}
47+
}
48+
49+
}
50+
/*for(int i = 1 ; i <= 5 ; i++)
51+
cout<<"d["<<i<<"] = "<<d[i]<<endl;
52+
for(int i = 1 ; i <= 5 ; i++)
53+
cout<<"parent["<<i<<"] = "<<parent[i]<<endl;
54+
*/
55+
return 0;
56+
}

kruskal.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <map>
5+
#include <queue>
6+
#include <algorithm>
7+
8+
using namespace std;
9+
10+
typedef pair <int,int> pii;
11+
typedef vector <pii> vpii;
12+
13+
vpii graph[100];
14+
int id[100];
15+
int sz[100];
16+
void init(int n){
17+
for(int i = 0 ; i < n ; i++)
18+
{
19+
id[i] = i;
20+
sz[i] = 1;
21+
}
22+
}
23+
24+
int find(int p){
25+
while(p!=id[p]){
26+
p = id[p];
27+
}
28+
return p;
29+
}
30+
31+
bool conncted(int p , int q){
32+
return (find(p) == find(q));
33+
}
34+
35+
void join(int p , int q){
36+
int proot = find(p);
37+
int qroot = find(q);
38+
id[proot] = qroot;
39+
}
40+
41+
struct Edge{
42+
int weight;
43+
int either;
44+
int other;
45+
};
46+
struct comparator{
47+
bool operator()(const Edge &a, const Edge &b){
48+
return (a.weight > b.weight);
49+
}
50+
};
51+
int main(){
52+
priority_queue<Edge,vector<Edge>,comparator> Q;
53+
//sample graph
54+
Edge edge[8];
55+
graph[0].push_back(pii(26,2));graph[0].push_back(pii(38,4));graph[0].push_back(pii(58,6));graph[0].push_back(pii(16,7));
56+
graph[1].push_back(pii(36,2));graph[1].push_back(pii(29,3));graph[1].push_back(pii(32,5));graph[1].push_back(pii(19,7));
57+
graph[2].push_back(pii(26,0));graph[2].push_back(pii(36,1));graph[2].push_back(pii(17,3));graph[2].push_back(pii(40,6));graph[2].push_back(pii(34,7));
58+
graph[3].push_back(pii(29,1));graph[3].push_back(pii(17,2));graph[3].push_back(pii(52,6));
59+
graph[4].push_back(pii(38,0));graph[4].push_back(pii(35,5));graph[4].push_back(pii(93,6));graph[4].push_back(pii(37,7));
60+
graph[5].push_back(pii(32,1));graph[5].push_back(pii(35,4));graph[5].push_back(pii(28,7));
61+
graph[6].push_back(pii(58,0));graph[6].push_back(pii(40,2));graph[6].push_back(pii(52,3));graph[6].push_back(pii(93,4));
62+
graph[7].push_back(pii(16,0));graph[7].push_back(pii(19,1));graph[7].push_back(pii(34,2));graph[7].push_back(pii(37,4));graph[7].push_back(pii(28,5));
63+
64+
//sample graph ends
65+
edge[0].either = 0; edge[0].other = 2; edge[0].weight = 26; edge[0].either = 0; edge[0].other = 4; edge[0].weight = 38;
66+
edge[0].either = 0; edge[0].other = 6; edge[0].weight = 58; edge[0].either = 0; edge[0].other = 7; edge[0].weight = 16;
67+
68+
69+
Q.push()
70+
return 0;
71+
}

lcs.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<iostream>
2+
#include<cstdio>
3+
#include<string>
4+
#include<cstring>
5+
6+
using namespace std;
7+
8+
int LCS[512][512];
9+
int findlcs(string s1,string s2)
10+
{
11+
int m=s1.size();
12+
int n=s2.size();
13+
for (int i = 0 ; i <= m ; i++){
14+
for (int j = 0 ; j <= n ; j++){
15+
if (i == 0 || j == 0)
16+
LCS[i][j] = 0;
17+
else if(s1[i-1] == s2[j-1])
18+
LCS[i][j] = 1 + LCS[i-1][j-1];
19+
else
20+
LCS[i][j] = max(LCS[i-1][j] , LCS[i][j-1]);
21+
}
22+
}
23+
return LCS[m][n];
24+
}
25+
int main()
26+
{
27+
string s1="AGCAT";
28+
string s2="GAC";
29+
int ans=findlcs(s1,s2);
30+
cout << ans <<endl;
31+
return 0;
32+
33+
}

linklist.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
3+
class Node{
4+
int data;
5+
Node next=null;
6+
};
7+
class linklist{
8+
public static void main (string[] Args){
9+
Node node1=new Node();
10+
Node node2=new Node();
11+
Node node3=new Node();
12+
node1.data=1;
13+
node2.data=2;
14+
node3.data=3;
15+
node1.next=node2;
16+
node2.next=node3;
17+
node3.next=null;
18+
while (Node.next !=null){
19+
System.out.println(Node.data);
20+
}
21+
}
22+
}

lis.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
4+
using namespace std;
5+
int lis[101];
6+
int arr[101];
7+
8+
int findLIS (int *arr, int n){
9+
int mx = 0;
10+
for (int i = 0 ; i < n ; i++){
11+
lis[i] = 1;
12+
}
13+
for (int i = 1 ; i < n ; i++){
14+
for (int j = 0 ; j < i ; j++){
15+
if (arr[j] < arr[i] && lis[i] < lis[j]+1)
16+
lis[i] = lis[j] + 1;
17+
}
18+
}
19+
for (int i = 0 ; i < n ; i++){
20+
if(mx < lis[i])
21+
mx = lis[i];
22+
}
23+
return mx;
24+
}
25+
26+
int main()
27+
{
28+
int arr[] = {29,22,25,33};
29+
int ans = findLIS(arr,4);
30+
cout<<"LIS = "<<ans<<endl;
31+
return 0;
32+
}

matrixexp.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
using namespace std;
5+
6+
typedef vector <vector <int> > mat;
7+
mat mul(mat a , mat b){
8+
mat c;
9+
c.resize(2,vector<int>(2,0));
10+
for (int i = 0 ; i < 2 ; i++){
11+
for(int j = 0 ; j < 2 ; j++){
12+
c[i][j] = 0;
13+
for(int k = 0 ; k < 2 ; k++){
14+
c[i][j] +=( a[i][k]*b[k][j]);
15+
}
16+
}
17+
}
18+
return c;
19+
}
20+
mat power(mat a , int k){
21+
if(k == 1)
22+
return a;
23+
if(!(k%2))
24+
return (mul(power(a,k/2) , power(a,k/2)));
25+
if(k%2)
26+
return (mul(power(a,k/2) , power(a,(k/2)+1)));
27+
}
28+
29+
int main(){
30+
mat M;
31+
M.resize(2 , vector <int>(2,0) );
32+
M[0][0] = 1; M[0][1] = 1;
33+
M[1][0] = 1; M[1][1] = 0;
34+
M = power(M,4);
35+
int ans = M[0][1];
36+
cout<<"fib = "<<ans<<endl;
37+
38+
return 0;
39+
}

maxsubsequence.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
5+
using namespace std;
6+
int S[101];
7+
int main()
8+
{
9+
S[0]=0;
10+
int a[]={-2,-3,4,-1,-2,1,5,-3};
11+
S[1]=a[0];
12+
return 0;
13+
14+
}

quickfind.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Quick- Find
2+
//Eager Approach
3+
4+
include <iostream>
5+
6+
using namespace std;
7+
int id[101];
8+
9+
void init(int n){
10+
for (int i = 0 ; i < n ; i++){
11+
id[i] = i;
12+
}
13+
}
14+
15+
bool isconnected (int p , int q){
16+
return (id[p] == id[q]);
17+
}
18+
19+
void union (int p , int q){
20+
int pid = id[p];
21+
int qid = id[q];
22+
for(int i = 0 i < n ; i++){
23+
if(id[i] == pid)
24+
id[i] = qid;
25+
}
26+
}
27+
int main()
28+
{
29+
init(10);
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)