-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion-advance-level.cpp
More file actions
453 lines (379 loc) Β· 12 KB
/
recursion-advance-level.cpp
File metadata and controls
453 lines (379 loc) Β· 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// πΈ 1. Backtracking βββ (MOST IMPORTANT)
// π Core Idea
// π Try all possibilities + undo (backtrack)
// Answer=All valid configurations
// Used to generate all possibilities
// π Recursive Relation
// f(i)=f(i+1)+f(i+1)
// π Explore both possibilities at each index
// Base case:
// f(n)=1
// When we have fixed all elements, we have found a valid configuration, so we return
// 1 to count this valid configuration; otherwise, we return 0.
// πΉ (A) N-Queens
// π Idea
// Place queens such that:
// No same row
// No same column
// No diagonal conflict
// Total configurations:
// n!
// Base case:
// When we have placed all queens, we have found a valid configuration, so we return
// 1 to count this valid configuration; otherwise, we return 0.
// f(i)=f(i+1)+f(i+1)+β¦+f(i+n-1)
// π Explore all possibilities by placing queens at each index
// Base case:
// f(n)=1
// When we have placed all queens, we have found a valid configuration, so we return
// 1 to count this valid configuration; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// bool issafe(int row,int col,vector<string>&board,int n){
// int r=row,c=col;
// // Check upper diagonal
// while(r>=0 && c>=0){
// if(board[r][c]=='Q') return false;
// r--;
// c--;
// }
// // Check left
// c=col,r=row;
// while(c>=0){
// if(board[r][c]=='Q') return false;
// c--;
// }
// // Check lower diagonal
// r=row,c=col;
// while(r<n && c>=0){
// if(board[r][c]=='Q') return false;
// r++;
// c--;
// }
// return true;
// }
// void solve(int col,vector<string>&board,int n,vector<vector<string>>&ans){
// if(col==n){
// ans.push_back(board);
// return;
// }
// for(int row=0;row<n;row++){
// if(issafe(row,col,board,n)){
// board[row][col]='Q';
// solve(col+1,board,n,ans);
// board[row][col]='.';// Backtrack
// }
// }
// }
// int main(){
// int n;
// cin>>n;
// vector<string>board(n,string(n,'.'));
// vector<vector<string>>ans;
// solve(0,board,n,ans);
// for(auto it:ans){
// for(auto i:it){
// cout<<i<<endl;
// }
// cout<<endl;
// }
// return 0;
// }
// πΉ (B) Rat in a Maze
// π Idea
// Move in 4 directions β avoid visited
// Total paths:
// 4^(n*m)
// Base case:
// When we reach the destination, we have found a valid path, so we return 1 to count this valid path; otherwise, we return 0.
// f(i,j)=f(i+1,j)+f(i-1,j)+f(i,j+1)+f(i,j-1)
// π Explore all possibilities by moving in 4 directions at each step
// Base case:
// f(n,m)=1
// When we reach the destination, we have found a valid path, so we return 1 to count this valid path; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// void solveMaze(int i,int j,vector<vector<int>>&maze,vector<vector<int>>&visited,int n,int m,int &cnt,string path){
// if(i==n-1 && j==m-1){
// cnt++;
// cout<<path<<endl;
// return;
// }
// if(i<0 || j<0 || i>=n || j>=m || maze[i][j]==0 || visited[i][j]==1){
// return ;
// }
// visited[i][j]=1;
// // Down
// solveMaze(i+1,j,maze,visited,n,m,cnt,path+'D');
// // Up
// solveMaze(i-1,j,maze,visited,n,m,cnt,path+'U');
// // Right
// solveMaze(i,j+1,maze,visited,n,m,cnt,path+'R');
// // Left
// solveMaze(i,j-1,maze,visited,n,m,cnt,path+'L');
// visited[i][j]=0;// Backtrack
// maze[i][j]=1;// Backtrack
// }
// int main(){
// int n,m;
// cin>>n>>m;
// vector<vector<int>>maze(n,vector<int>(m));
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// cin>>maze[i][j];
// }
// }
// vector<vector<int>>visited(n,vector<int>(m,0));
// int cnt=0;
// solveMaze(0,0,maze,visited,n,m,cnt,"");
// cout<<cnt<<endl;
// return 0;
// }
// πΉ (C) Sudoku Solver
// π Idea
// Fill empty cells with valid digits (1β9)
// Total configurations:
// 9^(n*n)
// Base case:
// When we have filled all cells, we have found a valid configuration, so we return 1 to count this valid configuration; otherwise, we return 0.
// f(i,j)=f(i+1,j)+f(i-1,j)+f(i,j+1)+f(i,j-1)
// π Explore all possibilities by filling digits at each cell
// Base case:
// f(n,n)=1
// When we have filled all cells, we have found a valid configuration, so we return 1 to count this valid configuration; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// bool isValid(vector<vector<char>>&board,int row,int col,char c){
// for(int i=0;i<9;i++){
// if(board[row][i]==c) return false;
// if(board[i][col]==c) return false;
// if(board[3*(row/3)+i/3][3*(col/3)+i%3]==c) return false;
// }
// return true;
// }
// bool solveSudoku(vector<vector<char>>&board){
// for(int i=0;i<9;i++){
// for(int j=0;j<9;j++){
// if(board[i][j]=='.'){
// for(char c='1';c<='9';c++){
// if(isValid(board,i,j,c)){
// board[i][j]=c;
// if(solveSudoku(board)) {
// return true;
// }
// board[i][j]='.'; // Backtrack
// }
// }
// return false; // No valid digit found, trigger backtracking
// }
// }
// }
// return true; // Solved
// }
// int main(){
// vector<vector<char>>board(9,vector<char>(9));
// for(int i=0;i<9;i++){
// for(int j=0;j<9;j++){
// cin>>board[i][j];
// }
// }
// if(solveSudoku(board)){
// for(int i=0;i<9;i++){
// for(int j=0;j<9;j++){
// cout<<board[i][j]<<" ";
// }
// cout<<endl;
// }
// }
// else{
// cout<<"No solution exists"<<endl;
// }
// return 0;
// }
// πΈ 2. Divide and Conquer
// π Idea
// Divide β Solve β Merge
// π Recursive Relation
// f(i)=f(i+1)+f(i+1)
// π Explore both possibilities at each index
// Base case:
// f(n)=1
// When we have solved all subproblems, we have found a valid solution, so we return
// 1 to count this valid solution; otherwise, we return 0.
//approach is similar to backtracking but we combine results from subproblems instead of exploring all possibilities at each index.
//mathmatical Idea and implimentation is similar to backtracking but we combine results from subproblems instead of exploring all possibilities at each index.
// πΉ 1. Merge Sort
// π Idea
// Divide array into halves β sort each half β merge
// Total operations:
// n log n
// Base case:
// #include<bits/stdc++.h>
// using namespace std;
// void merge(vector<int>&arr,int left,int m,int right){
// vector<int>temp;
// int i=left,j=m+1;
// while(i<=m && j<=right){
// if(arr[i]<=arr[j]){
// temp.push_back(arr[i++]);
// }
// else{
// temp.push_back(arr[j++]);
// }
// }
// while(i<=m) temp.push_back(arr[i++]);
// while(j<=right) temp.push_back(arr[j++]);
// for(int k=left;k<=right;k++){
// arr[k]=temp[k-left];
// }
// }
// void mergesort(vector<int>& arr,int left,int right){
// if(left>=right) return;
// int mid=(left+right)/2;
// mergesort(arr,left,mid);
// mergesort(arr,mid+1,right);
// merge(arr,left,mid,right);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// mergesort(arr,0,n-1);
// for(int x:arr){
// cout<<x<<" ";
// }
// cout<<endl;
// return 0;
// }
// πΉ 2. Quick Sort
// π Idea
// Choose pivot β partition β sort partitions
// Total operations:
// n log n (average), n^2 (worst)
// Base case:
// When we have sorted all partitions, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// f(left,right)=f(left,pivot-1)+f(pivot+1,right)
// π Explore all possibilities by choosing different pivots at each step
// Base case:
// f(left,right)=1
// When we have sorted all partitions, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// void quicksort(vector<int>&arr,int left,int right){
// if(left>=right) return;
// int pivot=arr[right];
// int i=left;
// for(int j=left;j<right;j++){
// if(arr[j]<pivot){
// swap(arr[i],arr[j]);
// i++;
// }
// }
// swap(arr[i],arr[right]);
// quicksort(arr,left,i-1);
// quicksort(arr,i+1,right);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// quicksort(arr,0,n-1);
// for(int x:arr){
// cout<<x<<" ";
// }
// cout<<endl;
// return 0;
// }
// πΈ 3. Recursion + Memoization (DP Intro)
// πΉ Fibonacci Optimized
// π Idea
// Store previously computed results to avoid redundant calculations
// Total operations:
// n (linear)
// Base case:
// When we have computed the Fibonacci number for all n, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// f(n)=f(n-1)+f(n-2)
// π Explore all possibilities by computing Fibonacci numbers at each index and storing results
// Base case:
// f(n)=1
// When we have computed the Fibonacci number for all n, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// int fibonacci(int n,vector<int>&dp){
// if(n==0) return 0;
// if(n==1) return 1;
// if(dp[n]!=-1) return dp[n];
// return dp[n]=fibonacci(n-1,dp) +fibonacci(n-2,dp);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>dp(n+1,-1);
// cout<<fibonacci(n,dp)<<endl;
// return 0;
// }
// πΉ Climbing Stairs
// π Idea
// f(n)=f(nβ1)+f(nβ2)
// Total ways:
// n (linear)
// Base case:
// When we have computed the number of ways to climb all stairs, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// f(n)=f(n-1)+f(n-2)
// π Explore all possibilities by computing ways to climb stairs at each index and storing results
// Base case:
// f(n)=1
// When we have computed the number of ways to climb all stairs, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// int climbStairs(int n,vector<int>&dp){
// if(n==0) return 1;
// if(n<0) return 0;
// if(dp[n]!=-1) return dp[n];
// return dp[n]=climbStairs(n-1,dp) +climbStairs(n-2,dp);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>dp(n+1,-1);
// cout<<climbStairs(n,dp)<<endl;
// return 0;
// }
// πΈ 4. Recursion + Tabulation (DP Intro)
// πΉ Fibonacci Tabulation
// π Idea
// Build a table bottom-up to compute Fibonacci numbers
// Total operations:
// n (linear)
// Base case:
// When we have computed the Fibonacci number for all n, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// f(n)=f(n-1)+f(n-2)
// π Explore all possibilities by computing Fibonacci numbers at each index and storing results
// Base case:
// f(n)=1
// When we have computed the Fibonacci number for all n, we have found a valid solution, so we return 1 to count this valid solution; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// int fibonacci(int n){
// if(n==0) return 0;
// if(n==1) return 1;
// vector<int>dp(n+1);
// dp[0]=0;
// dp[1]=1;
// for(int i=2;i<=n;i++){
// dp[i]=dp[i-1]+dp[i-2];
// }
// return dp[n];
// }
// int main(){
// int n;
// cin>>n;
// cout<<fibonacci(n)<<endl;
// return 0;
// }