-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0675-cut-off-trees-for-golf-event.js
More file actions
110 lines (93 loc) · 2.81 KB
/
0675-cut-off-trees-for-golf-event.js
File metadata and controls
110 lines (93 loc) · 2.81 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
/**
* Cut Off Trees For Golf Event
* Time Complexity: O(T * R * C)
* Space Complexity: O(R * C)
*/
const cutOffTree = (forestInput) => {
const rowCount = forestInput.length;
const columnCount = forestInput[0].length;
const treeCoordinates = [];
for (let rInitial = 0; rInitial < rowCount; rInitial++) {
for (let cInitial = 0; cInitial < columnCount; cInitial++) {
if (forestInput[rInitial][cInitial] > 1) {
treeCoordinates.push([
forestInput[rInitial][cInitial],
rInitial,
cInitial,
]);
}
}
}
treeCoordinates.sort((coordA, coordB) => coordA[0] - coordB[0]);
let currentCoordinates = [0, 0];
let totalDistanceAccumulated = 0;
for (const singleTreeData of treeCoordinates) {
const targetRowPosition = singleTreeData[1];
const targetColPosition = singleTreeData[2];
const pathLengthCalculated = calculateBfsDistance(
currentCoordinates[0],
currentCoordinates[1],
targetRowPosition,
targetColPosition,
forestInput,
);
if (pathLengthCalculated === -1) {
return -1;
}
totalDistanceAccumulated += pathLengthCalculated;
currentCoordinates = [targetRowPosition, targetColPosition];
}
return totalDistanceAccumulated;
};
const calculateBfsDistance = (
startRowCoord,
startColCoord,
endRowCoord,
endColCoord,
gridData,
) => {
if (startRowCoord === endRowCoord && startColCoord === endColCoord) {
return 0;
}
const gridRows = gridData.length;
const gridCols = gridData[0].length;
const movementDirections = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const bfsExplorationQueue = [[startRowCoord, startColCoord, 0]];
const visitedTracker = new Set();
visitedTracker.add(`${startRowCoord},${startColCoord}`);
let advancePointer = 0;
while (advancePointer < bfsExplorationQueue.length) {
const [currentRowVisit, currentColVisit, currentTraversalDistance] =
bfsExplorationQueue[advancePointer++];
for (const [deltaR, deltaC] of movementDirections) {
const nextRowVisit = currentRowVisit + deltaR;
const nextColVisit = currentColVisit + deltaC;
if (
nextRowVisit >= 0 &&
nextRowVisit < gridRows &&
nextColVisit >= 0 &&
nextColVisit < gridCols &&
gridData[nextRowVisit][nextColVisit] !== 0
) {
const uniquePositionKey = `${nextRowVisit},${nextColVisit}`;
if (!visitedTracker.has(uniquePositionKey)) {
if (nextRowVisit === endRowCoord && nextColVisit === endColCoord) {
return currentTraversalDistance + 1;
}
visitedTracker.add(uniquePositionKey);
bfsExplorationQueue.push([
nextRowVisit,
nextColVisit,
currentTraversalDistance + 1,
]);
}
}
}
}
return -1;
};