Skip to content

Commit 502f347

Browse files
committed
[leet] 79. Word Search [mid]
1 parent c65d4c5 commit 502f347

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

허현빈/5주차/260128-1.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number[][]}
4+
*/
5+
var merge = function (intervals) {
6+
const arr = intervals.sort((a, b) => {
7+
if (a[0] !== b[0]) {
8+
return a[0] - b[0];
9+
} else {
10+
return a[1] - b[1];
11+
}
12+
});
13+
const ans = [];
14+
let preStart = arr[0][0];
15+
let preEnd = arr[0][1];
16+
for (let i = 1; i < arr.length; i++) {
17+
const start = arr[i][0];
18+
const end = arr[i][1];
19+
if (start > preEnd) {
20+
ans.push([preStart, preEnd]);
21+
preStart = start;
22+
preEnd = end;
23+
} else {
24+
preEnd = Math.max(end, preEnd);
25+
}
26+
}
27+
ans.push([preStart, preEnd]);
28+
return ans;
29+
};

허현빈/5주차/260128-2.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
var exist = function (b, w) {
7+
const rows = b.length;
8+
const cols = b[0].length;
9+
const dir = [
10+
[1, 0],
11+
[-1, 0],
12+
[0, 1],
13+
[0, -1],
14+
];
15+
const visit = Array.from({ length: rows }, () => Array(cols).fill(false));
16+
const dfs = (y, x, count) => {
17+
if (count === w.length) return true;
18+
19+
if (
20+
y < 0 ||
21+
y >= rows ||
22+
x < 0 ||
23+
x >= cols ||
24+
visit[y][x] ||
25+
b[y][x] !== w[count]
26+
) {
27+
return false;
28+
}
29+
30+
visit[y][x] = true;
31+
for (let i = 0; i < 4; i++) {
32+
const ny = y + dir[i][1];
33+
const nx = x + dir[i][0];
34+
35+
if (dfs(ny, nx, count + 1)) return true;
36+
}
37+
visit[y][x] = false;
38+
return false;
39+
};
40+
41+
for (let i = 0; i < rows; i++) {
42+
for (let j = 0; j < cols; j++) {
43+
if (b[i][j] === w[0]) {
44+
if (dfs(i, j, 0)) return true;
45+
}
46+
}
47+
}
48+
49+
return false;
50+
};

0 commit comments

Comments
 (0)