-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_solution.js
120 lines (99 loc) · 3.23 KB
/
gpt_solution.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Runtime 117 ms Beats 81.9% Memory 51.5 MB Beats 48.34%
/**
* @param {number[][]} heights
* @return {number[][]}
*/
const pacificAtlantic = (heights) => {
if (heights.length === 0 || heights[0].length === 0) {
return [];
}
const m = heights.length;
const n = heights[0].length;
const pacificQueue = []; // Queue for cells that can flow to the Pacific Ocean
const atlanticQueue = []; // Queue for cells that can flow to the Atlantic Ocean
// Initialize visited arrays for both oceans
const pacificVisited = Array.from({ length: m }, () => new Array(n).fill(false));
const atlanticVisited = Array.from({ length: m }, () => new Array(n).fill(false));
// console.log("pacificVisited")
// console.log(pacificVisited)
// console.log("atlanticVisited")
// console.log(atlanticVisited)
// Add border cells to the respective queues and mark them as visited
for (let i = 0; i < m; i++) {
pacificQueue.push([i, 0]);
atlanticQueue.push([i, n - 1]);
pacificVisited[i][0] = true;
atlanticVisited[i][n - 1] = true;
}
for (let j = 0; j < n; j++) {
pacificQueue.push([0, j]);
atlanticQueue.push([m - 1, j]);
pacificVisited[0][j] = true;
atlanticVisited[m - 1][j] = true;
}
// NOTE: Form grids that wraps the sides that touch the ocean
console.log("pacificVisited")
console.log(pacificVisited)
console.log("pacificQueue")
console.log(pacificQueue)
console.log("atlanticVisited")
console.log(atlanticVisited)
console.log("atlanticQueue")
console.log(atlanticQueue)
// Perform BFS from the Pacific Ocean
bfs(pacificQueue, pacificVisited, heights);
// Perform BFS from the Atlantic Ocean
bfs(atlanticQueue, atlanticVisited, heights);
console.log("-----> After BFS")
console.log("pacificVisited")
console.log(pacificVisited)
console.log("pacificQueue")
console.log(pacificQueue)
console.log("atlanticVisited")
console.log(atlanticVisited)
console.log("atlanticQueue")
console.log(atlanticQueue)
// Find cells that can flow to both oceans
const result = [];
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (pacificVisited[i][j] && atlanticVisited[i][j]) result.push([i, j]);
}
}
return result;
};
const bfs = (queue, visited, heights) => {
const m = heights.length;
const n = heights[0].length;
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; // Right, Down, Left, Up
while (queue.length > 0) {
const [row, col] = queue.shift();
for (const [dx, dy] of directions) {
const newRow = row + dx;
const newCol = col + dy;
// Check if the new cell is within bounds
if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n) {
// Check if the new cell is unvisited and its height is not greater than the current cell
if (!visited[newRow][newCol] && heights[newRow][newCol] >= heights[row][col]) {
visited[newRow][newCol] = true;
queue.push([newRow, newCol]);
}
}
}
}
};
// let x = pacificAtlantic([
// [1, 2, 3],
// [6, 5, 4],
// [2, 7, 2],
// ]);
let x = pacificAtlantic([
[1, 2, 2, 3, 5],
[3, 2, 3, 4, 4],
[2, 4, 5, 3, 1],
[6, 7, 1, 4, 5],
[5, 1, 1, 2, 4]
]);
// let x = pacificAtlantic([[1]]);
console.log("Result");
console.log(x);