Skip to content

Commit a4d05c4

Browse files
authored
Merge pull request #152 from sir-gon/problem0011-refactor
[REFACTOR] problem0011 complexity reduced.
2 parents 023bb12 + 42aab6d commit a4d05c4

File tree

1 file changed

+23
-69
lines changed

1 file changed

+23
-69
lines changed

src/projecteuler/problem0011.ts

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,35 @@ import { logger as console } from '../logger';
22
import { maximum } from './helpers';
33

44
function problem0011(_squareMatrix: number[][], _interval: number): number {
5-
const top = _squareMatrix.length;
6-
75
let max = 0;
8-
let acum;
9-
10-
for (let i = 0; i < top; i++) {
11-
for (let j = 0; j < top; j++) {
12-
acum = 1;
136

14-
if (i < top - (_interval - 1)) {
7+
const quadrantSize = _interval;
8+
const matrixLimit = _squareMatrix.length - (_interval - 1);
9+
10+
for (let i = 0; i < matrixLimit; i++) {
11+
for (let j = 0; j < matrixLimit; j++) {
12+
let verticalAcum = 1;
13+
let horizontalAcum = 1;
14+
let diag1Acum = 1;
15+
let diag2Acum = 1;
16+
console.debug(`start point: i ${i}, j: ${j}`);
17+
for (let k = 0; k < quadrantSize; k++) {
18+
console.debug(`vertical coordinate: (i, j) = (${i + k}, ${j})`);
19+
console.debug(`horizontal coordinate: (i, j) = ($i}, ${j + k})`);
20+
console.debug(`diag1 coordinate: (i, j) = (${i + k}, ${j + k})`);
1521
console.debug(
16-
`---- VERTICAL ------------------------------------------`
22+
`diag2 coordinate: (i, j) = (${i + k}, ${j + (quadrantSize - 1) - k})`
1723
);
18-
// vertical
19-
for (let k = 0; k < _interval; k++) {
20-
console.debug(
21-
`row: i ${i + k}, column: ${j}, _interval ${k} => ${
22-
_squareMatrix[i + k][j]
23-
}`
24-
);
25-
26-
acum *= _squareMatrix[i + k][j];
27-
}
28-
29-
max = maximum(acum, max);
30-
}
3124

32-
acum = 1;
33-
if (j < top - (_interval - 1)) {
34-
console.debug(
35-
`---- HORIZONTAL ----------------------------------------`
36-
);
37-
// horizontal
38-
for (let k = 0; k < _interval; k++) {
39-
console.debug(
40-
`row: i ${i}, column: ${j + k} => ${_squareMatrix[i][j + k]}`
41-
);
42-
acum *= _squareMatrix[i][j + k];
43-
}
44-
45-
max = maximum(acum, max);
46-
}
47-
48-
acum = 1;
49-
if (i + (_interval - 1) < top && j + (_interval - 1) < top) {
50-
// diagonal top left -> bottom right
51-
console.debug(
52-
`---- DIAG \\ ---------------------------------------------`
53-
);
54-
for (let k = 0; k < _interval; k++) {
55-
console.debug(
56-
`diag: (${i + k}, ${j + k}) => ${_squareMatrix[i + k][j + k]}`
57-
);
58-
acum *= _squareMatrix[i + k][j + k];
59-
}
60-
61-
max = maximum(acum, max);
62-
}
63-
64-
acum = 1;
65-
if (i + (_interval - 1) < top && j + (_interval - 1) < top) {
66-
// diagonal top rigth -> bottom left
67-
console.debug(
68-
`---- DIAG / ---------------------------------------------`
69-
);
70-
for (let k = 0; k < _interval; k++) {
71-
console.debug(
72-
`diag: (${i + k}, ${j + (_interval - 1) - k}) => ${
73-
_squareMatrix[i + k][j + (_interval - 1) - k]
74-
}`
75-
);
76-
acum *= _squareMatrix[i + k][j + (_interval - 1) - k];
77-
}
25+
verticalAcum *= _squareMatrix[i + k][j];
26+
horizontalAcum *= _squareMatrix[i][j + k];
27+
diag1Acum *= _squareMatrix[i + k][j + k];
28+
diag2Acum *= _squareMatrix[i + k][j + (quadrantSize - 1) - k];
7829

79-
max = maximum(acum, max);
30+
max = maximum(verticalAcum, max);
31+
max = maximum(horizontalAcum, max);
32+
max = maximum(diag1Acum, max);
33+
max = maximum(diag2Acum, max);
8034
}
8135
}
8236
}

0 commit comments

Comments
 (0)