Skip to content

Commit fac0044

Browse files
Mohit JainMohit Jain
Mohit Jain
authored and
Mohit Jain
committed
mixed
1 parent b240f36 commit fac0044

23 files changed

+664
-10
lines changed

CountBinary

33.2 KB
Binary file not shown.

CountBinary.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int CountStrings(int n,int dp[][2]){
6+
7+
if(n == 0) return 1;
8+
9+
if(n == 1) return 2;
10+
11+
return CountStrings(n-1) + CountStrings(n-2);
12+
13+
14+
15+
}
16+
17+
int main() {
18+
19+
20+
#ifndef ONLINE_JUDGE
21+
// for getting input from input.txt
22+
freopen("input.txt", "r", stdin);
23+
// for writing output to output.txt
24+
freopen("output.txt", "w", stdout);
25+
#endif
26+
27+
int n;
28+
29+
cin >>n ;
30+
int dp[n+1][2]={};
31+
cout<<CountStrings(n,dp);
32+
33+
return 0;
34+
35+
}

DpIntro

100644100755
33.1 KB
Binary file not shown.

DpIntro.cpp

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int fib(int n) {
6+
if(n == 0 or n == 1){
7+
return n;
8+
}
9+
int ans;
10+
11+
ans = fib(n-1) + fib(n-2);
12+
13+
return ans;
14+
}
15+
int topDownFib(int n,int dp[]){
16+
17+
if(n==0 or n==1) return n;
18+
19+
if(dp[n]!=0) return dp[n];
20+
int ans = topDownFib(n-1,dp) + topDownFib(n-2,dp);
21+
22+
return dp[n] = ans;
23+
}
24+
25+
int bottomUpFib(int n){
26+
int dp[100];
27+
dp[1] = 1;
28+
for(int i=2;i<=n;i++){
29+
dp[i] = dp[i-1]+dp[i-2];
30+
}
31+
32+
return dp[n];
33+
}
34+
int fibSpaceOpt(int n) {
35+
if(n==o or n == 1) return n;
36+
37+
int a =0;
38+
int b = 1;
39+
int c;
40+
for(int i=2; i <=n; i++) {
41+
42+
c= a +b;
43+
a = b;
44+
b = c;
45+
}
46+
return c;
47+
}
48+
// Min steps to one
49+
// n-> n/3,n/2,n-1
50+
int minStepsOne(int n,int dp[]) {
51+
52+
// Base Case
53+
if(n == 1){
54+
return 0;
55+
}
56+
//Lookup
57+
if(dp[n]!=0){
58+
return dp[n];
59+
}
60+
// rec case
61+
62+
int op1,op2,op3;
63+
op1 = op2 = op3 = INT_MAX;
64+
65+
if(n%3 == 0) {
66+
op1 = minStepsOne(n/3,dp) + 1;
67+
}
68+
if(n%2 ==0) {
69+
op2 = minStepsOne(n/2,dp) + 1;
70+
71+
}
72+
op3 = minStepsOne(n-1,dp) + 1;
73+
74+
int ans = min(min(op1,op2),op3);
75+
76+
return dp[n] = ans;
77+
78+
}
79+
int minStepsBottomUp(int n){
80+
81+
int dp[100] = {0};
82+
dp[1] = 0;
83+
for(int i =2; i<=n; i++){
84+
int op1,op2,op3;
85+
op1 = op2 = op3 = INT_MAX;
86+
if(n%3 ==0){
87+
op[1] = dp[i/3];
88+
}
89+
if(n%2==0){
90+
op[2] = dp[i/2];
91+
92+
};
93+
op[3] = dp[i-1];
94+
95+
dp[i] = min(min(op1,op2),op3);
96+
}
97+
return dp[n];
98+
99+
100+
}
101+
// top down - rec solution
102+
103+
int minCoins(int N,int coins[],int T,int dp[]){
104+
if(n == 0) return 0;
105+
106+
if(dp[n]! = 0) return dp[n];
107+
int ans = INT_MAX;
108+
109+
for(int i =0; i <T; i++) {
110+
if(n-coins[i]>=0) {
111+
int subProb = minCoins(n-coins[i],coins,T,dp);
112+
ans = min(ans,subProb + 1);
113+
}
114+
}
115+
return dp[n] = ans;
116+
117+
}
118+
int minCoinsBottomUp(int N,int coins[],int T){
119+
120+
int dp[100] = {0};
121+
122+
for(int n = 1; n<=N; n++) {
123+
dp[n] = INT_MAX;
124+
125+
for(int i =0; i <T; i++) {
126+
127+
if(n-coins[i]>=0) {
128+
int subprob = dp[n-coins[i]];
129+
dp[n] = min(dp[n],subprob+1);
130+
}
131+
}
132+
}
133+
return dp[N];
134+
}
135+
// Top down approach
136+
int maxProfitSellingBottles(int wines[],int i,
137+
int j,int y,int dp[][100]){
138+
// Base Case
139+
140+
if(i>j) return 0;
141+
if(dp[i][j]!=0) return dp[i][j];
142+
143+
int op1 = wines[i]*y + profit(wines,i+1,j,y+1,dp);
144+
int op2 = wines[j]*y + profit(wines,i,j-1,y+1,dp);
145+
146+
return dp[i][j] = max(op1,op2);
147+
148+
149+
}
150+
// Bottom up approach
151+
int maxProfitSellingBottlesBU(int wines[],int y) {
152+
153+
int dp[100][100] = {};
154+
int year = y;
155+
for(int i =0; i<n; i++){
156+
dp[i][i] = year * arr[i];
157+
}
158+
for(int len = 2; len <=n; len++){
159+
int srt = 0;
160+
int end = n - len ;
161+
while(srt <= end) {
162+
int endwindow = srt + len - 1;
163+
dp[srt][endwindow] = max(
164+
wines[srt]*year + dp[srt+1][endwindow],
165+
wines[endwindow]*year +
166+
dp[srt][endwindow-1]);
167+
++srt;
168+
}
169+
-- year;
170+
}
171+
172+
for(int i = 0; i <n; ++i){
173+
for(int j =0; j <n; ++j){
174+
cout<<setw(3)<<dp[i][j]<<" ";
175+
}
176+
cout<<endl;
177+
}
178+
return dp[0][n-1]
179+
}
180+
181+
int main() {
182+
183+
184+
#ifndef ONLINE_JUDGE
185+
// for getting input from input.txt
186+
freopen("input.txt", "r", stdin);
187+
// for writing output to output.txt
188+
freopen("output.txt", "w", stdout);
189+
#endif
190+
191+
int n;
192+
cin>>n;
193+
194+
cout<<fib(n);
195+
196+
return 0;
197+
}

DuplicateCharacterFormatting

40.8 KB
Binary file not shown.

DuplicateCharacterFormatting.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
void duplicateCharFormatting(char a[], int i){
5+
6+
if(a[i]=='\0' or a[i+1] == '\0') return;
7+
8+
if(a[i] == a[i+1]) {
9+
int j = i + 1;
10+
11+
while(a[j] != '\0' ){
12+
j++;
13+
}
14+
while(j >= i +1){
15+
a[j+1] = a[j];
16+
j--;
17+
}
18+
a[i+1] = '*';
19+
a[i+2] = a[i];
20+
duplicateCharFormatting(a,i+3);
21+
}
22+
else{
23+
duplicateCharFormatting(a,i+1);
24+
}
25+
26+
}
27+
int main() {
28+
29+
#ifndef ONLINE_JUDGE
30+
// for getting input from input.txt
31+
freopen("input.txt", "r", stdin);
32+
// for writing output to output.txt
33+
freopen("output.txt", "w", stdout);
34+
#endif
35+
36+
char ch[10000];
37+
38+
cin>>ch;
39+
duplicateCharFormatting(ch,0);
40+
cout<<ch<<endl;
41+
42+
return 0;
43+
}

MatChainMul.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main() {
6+
7+
8+
#ifndef ONLINE_JUDGE
9+
// for getting input from input.txt
10+
freopen("input.txt", "r", stdin);
11+
// for writing output to output.txt
12+
freopen("output.txt", "w", stdout);
13+
#endif
14+
15+
16+
17+
18+
return 0;
19+
}

MaxSubSum.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// bottom up approach
6+
int maxSum(int a[], int n){
7+
8+
int dp[100] = {0};
9+
10+
dp[0] = a[0]>0 ?a[0]:0;
11+
int max_so_far = dp[0];
12+
for(int i = 1; i <n;i++) {
13+
dp[i] = dp[i-1] + a[i];
14+
if(dp[i]<0) dp[i] = 0;
15+
16+
max_so_far = max(dp[i],max_so_far);
17+
}
18+
return max_so_far;
19+
}
20+
// topDown approach - HOMEWORK
21+
22+
int main() {
23+
24+
int n;
25+
cin>>n;
26+
int a[n];
27+
28+
for(int i =0 ; i <n; i++) cin>>a[i];
29+
30+
cout<<maxSum(a,n)<<endl;
31+
32+
return 0;
33+
34+
}

Non_Perm_App_Form_2021_v2-f1e92.docx

124 KB
Binary file not shown.

Tiling-2rec

34.7 KB
Binary file not shown.

Tiling-2rec.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define mod 1000000007
4+
int findTotalWays(int n,int m,int dp[]){
5+
if(n ==0) return dp[n] = 1;
6+
if(n<m) {
7+
8+
return dp[n] = 1;}
9+
if(dp[n]!=0) { return dp[n];}
10+
11+
dp[n] = (findTotalWays(n-1,m,dp)%mod + findTotalWays(n-m,m,dp)%mod)%mod;
12+
return dp[n];
13+
14+
}
15+
int main() {
16+
17+
#ifndef ONLINE_JUDGE
18+
// for getting input from input.txt
19+
freopen("input.txt", "r", stdin);
20+
// for writing output to output.txt
21+
freopen("output.txt", "w", stdout);
22+
#endif
23+
int t;
24+
cin>>t;
25+
while(t--) {
26+
int dp[100000] = {0};
27+
int n,m;
28+
cin>>n>>m;
29+
cout<<findTotalWays(n,m,dp)<<endl;
30+
}
31+
32+
return 0;
33+
34+
35+
}

TowerOfHanoi.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ int main() {
3535

3636
binary consist of only 0 and 1
3737

38-
pehle col vala loop h
39-
use sahi karo
40-
code m
41-
mera code dekho idhar
42-
4338
map<char,int> a[100];
4439

4540
for(int i=0; i< str.len(); i++) {
@@ -69,6 +64,5 @@ int main() {
6964
retrun 0;
7065
}
7166

72-
just iterate matrix and check whether it is 1 in column then it would return answer O(n2)
7367

7468

TreeIntro

39.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)