forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.js
More file actions
32 lines (32 loc) · 1.04 KB
/
code.js
File metadata and controls
32 lines (32 loc) · 1.04 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
tracer._print('values = [');
for (var i = 0; i < D.length; i++) {
tracer._print(' [' + D[i].join(', ') + ']');
}
tracer._print(']');
tracer._pace(200);
var N = DP.length;
var M = DP[0].length;
for (var i = 0; i < N; i++) {
for (var j = 0; j < M; j++) {
tracer._sleep();
if (i == 0 && j == 0) {
tracer._select(i, j);
DP[i][j] = D[i][j];
tracer._deselect(i, j);
} else if (i == 0) {
tracer._select(i, j - 1);
DP[i][j] = DP[i][j - 1] + D[i][j];
tracer._deselect(i, j - 1);
} else if (j == 0) {
tracer._select(i - 1, j);
DP[i][j] = DP[i - 1][j] + D[i][j];
tracer._deselect(i - 1, j);
} else {
tracer._selectSet([{x: i, y: j - 1}, {x: i - 1, y: j}]);
DP[i][j] = Math.max(DP[i][j - 1], DP[i - 1][j]) + D[i][j];
tracer._deselectSet([{x: i, y: j - 1}, {x: i - 1, y: j}]);
}
tracer._notify(i, j);
}
}
tracer._print('max = ' + DP[N - 1][M - 1]);