Repository navigation
Expand file tree
/
Copy pathrecursion-proplems.js
More file actions
131 lines (101 loc) · 2.75 KB
/
Copy pathrecursion-proplems.js
File metadata and controls
131 lines (101 loc) · 2.75 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
// recursion
// a function is said to be a recursive if it calls itself.
// the following function should print statment n times recursivly
function sayIamRecursive(n) {
// Base case
if (n < 1) return;
// logic
console.log("I am recursive");
// sub problem - never go to infinity
sayIamRecursive(--n);
}
// sayIamRecursive(3)
// calculate factorial
function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
//factorial(3)
function printLine(n) {
var s = '';
if (n < 1) {
s += "\n"; return s;
};
return s += "*" + printLine(n - 1)
}
function triangle(n) {
if (n < 1) return;
triangle(n - 1)
console.log(printLine(n));
}
// triangle(10)
// 4n+1 Series Next(N) if n is odd then n = 3 * n +1 . if n is even then n = n/2. if n==1, stop
// E.g. 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
// Given n, find length of 3n+1 sequence, E.g. F3_1(22) = 16 ;
function f3_1(n) {
// console.log(n)
if (n <= 1) return 1;
if (n % 2 === 0) return 1 + f3_1(n / 2);
else return 1 + f3_1(3 * n + 1);
}
// console.log( f3_1(22))
// recusive integer
function recusivNumber(n) {
console.log(n)
if (n <= 0) return '';
return (n % 10) + recusivNumber(Math.floor(n / 10));
}
//console.log(recusivNumber(12345678900))
// Given grid of positive numbers, start 0,0 and end at n,n . move only to right and sown - find path with sum of numbers is maximum.
/*
15
24
// 1 + 5 = 6
542
678
189
//5+6+7+8+9
*/
let grid = [[5,4,2],
[6,7,8],
[1,8,9]];
function maxPath(x,y){
const n = 3 ;
let d= 0, r = 0;
//calculate d
if(y+1 < n)
d = maxPath(x,y+1);
//calculate r
if(x+1 < n)
r = maxPath(x+1,y);
return grid[x][y] + Math.max(d , r) ;
}
// console.log(maxPath(0,0))
// maze , you have grid of .,x,d,s where you can walk on . and not x , find the minimum .s to reach d and you starting from s
// use backtracking and not revisit visited cell for a single path
var maze = [
['.','.','X','.','.'],
['.','.','.','.','.'],
['.','.','.','.','.'],
['.','.','.','.','.'],
['.','.','.','D','.']
]
const n2 = 5 ;
function findeMazeD(x,y){
if( x >= n2 || x < 0 || y >= n2 || y < 0 || maze[x][y] === 'X'|| maze[x][y] === 'V') return 10000000 ;
if(maze[x][y]==='D') {
return 1 ;
}
if(maze[x][y]=='.'){
maze[x][y]='V';
}
let resultD = (findeMazeD(x,y+1)) + 1 ; // down
let resultU = (findeMazeD(x,y-1)) + 1 ; // up
let resultL = (findeMazeD(x-1,y)) + 1 ; // left
let resultR = (findeMazeD(x+1,y)) + 1 ; // right
if(maze[x][y]=='V'){
maze[x][y]='.';
}
return Math.min(resultD ,resultU ,resultL ,resultR );
}
console.log(findeMazeD(0,1))