forked from dnp1204/daily-code-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem-68.js
98 lines (90 loc) · 2.42 KB
/
problem-68.js
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
/**
* Company: Google.
*
* On our special chessboard, two bishops attack each other if they share the same
* diagonal. This includes bishops that have another bishop located between them,
* i.e. bishops can attack through pieces.
*
* You are given N bishops, represented as (row, column) tuples on a M by M chessboard.
* Write a function to count the number of pairs of bishops that attack each other.
* The ordering of the pair doesn't matter: (1, 2) is considered the same as (2, 1).
*
* For example, given M = 5 and the list of bishops:
* (0, 0)
* (1, 2)
* (2, 2)
* (4, 0)
*
* The board would look like this:
* [b 0 0 0 0]
* [0 0 b 0 0]
* [0 0 b 0 0]
* [0 0 0 0 0]
* [b 0 0 0 0]
*
* You should return 2, since bishops 1 and 3 attack each other, as well as bishops 3
* and 4
*/
const countAttack = function(M, bishops = []) {
const table = initTable(M, bishops);
let total = 0;
for (const bishop of bishops) {
const [r, c] = bishop;
total =
total +
moveDownLeft(r, c, table) +
moveDownRight(r, c, table) +
moveTopLeft(r, c, table) +
moveTopRight(r, c, table);
}
console.log(table);
console.log(total);
return total;
};
const moveDownLeft = function(r, c, table) {
let attack = 0;
while (++r <= table.length && --c >= 0 && table[r][c] !== 1) {
if (table[r][c] === 'b') attack++;
if (table[r][c] === 0) table[r][c] = 1;
}
return attack;
};
const moveDownRight = function(r, c, table) {
let attack = 0;
while (++r < table.length && ++c < table[0].length && table[r][c] !== 1) {
if (table[r][c] === 'b') attack++;
if (table[r][c] === 0) table[r][c] = 1;
}
return attack;
};
const moveTopLeft = function(r, c, table) {
let attack = 0;
while (--r > 0 && --c > 0 && table[r][c] !== 1) {
if (table[r][c] === 'b') attack++;
if (table[r][c] === 0) table[r][c] = 1;
}
return attack;
};
const moveTopRight = function(r, c, table) {
let attack = 0;
while (--r > 0 && ++c < table[0].length && table[r][c] !== 1) {
if (table[r][c] === 'b') attack++;
if (table[r][c] === 0) table[r][c] = 1;
}
return attack;
};
const initTable = function(M, bishops) {
const table = [];
for (let i = 0; i < M; i++) {
table[i] = [];
for (let j = 0; j < M; j++) {
table[i][j] = 0;
}
}
for (const bishop of bishops) {
const [x, y] = bishop;
table[x][y] = 'b';
}
return table;
};
countAttack(5, [[0, 0], [1, 2], [2, 2], [4, 0]]);