Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8c732ee

Browse files
committedMar 9, 2025
Add solution #675
1 parent 86ffe87 commit 8c732ee

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@
508508
672|[Bulb Switcher II](./0672-bulb-switcher-ii.js)|Medium|
509509
673|[Number of Longest Increasing Subsequence](./0673-number-of-longest-increasing-subsequence.js)|Medium|
510510
674|[Longest Continuous Increasing Subsequence](./0674-longest-continuous-increasing-subsequence.js)|Easy|
511+
675|[Cut Off Trees for Golf Event](./0675-cut-off-trees-for-golf-event.js)|Hard|
511512
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
512513
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
513514
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* 675. Cut Off Trees for Golf Event
3+
* https://leetcode.com/problems/cut-off-trees-for-golf-event/
4+
* Difficulty: Hard
5+
*
6+
* You are asked to cut off all the trees in a forest for a golf event. The forest is represented
7+
* as an m x n matrix. In this matrix:
8+
* - 0 means the cell cannot be walked through.
9+
* - 1 represents an empty cell that can be walked through.
10+
* - A number greater than 1 represents a tree in a cell that can be walked through, and this number
11+
* is the tree's height.
12+
*
13+
* In one step, you can walk in any of the four directions: north, east, south, and west. If you are
14+
* standing in a cell with a tree, you can choose whether to cut it off.
15+
*
16+
* You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value
17+
* at its cell becomes 1 (an empty cell).
18+
*
19+
* Starting from the point (0, 0), return the minimum steps you need to walk to cut off all the
20+
* trees. If you cannot cut off all the trees, return -1.
21+
*
22+
* Note: The input is generated such that no two trees have the same height, and there is at least
23+
* one tree needs to be cut off.
24+
*/
25+
26+
/**
27+
* @param {number[][]} forest
28+
* @return {number}
29+
*/
30+
const cutOffTree = (forest) => {
31+
const treeHeights = forest.flat().filter((height) => height > 1).sort((a, b) => a - b);
32+
let currentPosition = [0, 0];
33+
let totalDistance = 0;
34+
35+
while (treeHeights.length) {
36+
const gridCopy = forest.map((row) => [...row]);
37+
const result = findDistance(currentPosition, treeHeights.shift(), gridCopy);
38+
if (result === null) return -1;
39+
const [nextPosition, distance] = result;
40+
currentPosition = nextPosition;
41+
totalDistance += distance;
42+
}
43+
return totalDistance;
44+
45+
function findDistance(startPosition, targetHeight, grid) {
46+
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];
47+
let queue = [startPosition];
48+
let distance = 0;
49+
50+
while (queue.length) {
51+
const nextLevel = [];
52+
53+
for (const [row, col] of queue) {
54+
if (grid[row][col] === targetHeight) return [[row, col], distance];
55+
if (!grid[row][col]) continue;
56+
57+
for (const [deltaRow, deltaCol] of directions) {
58+
const newRow = row + deltaRow;
59+
const newCol = col + deltaCol;
60+
if (
61+
newRow >= 0 && newRow < grid.length && newCol >= 0
62+
&& newCol < grid[0].length && grid[newRow][newCol]
63+
) {
64+
nextLevel.push([newRow, newCol]);
65+
}
66+
}
67+
grid[row][col] = 0;
68+
}
69+
distance += 1;
70+
queue = nextLevel;
71+
}
72+
return null;
73+
}
74+
};

0 commit comments

Comments
 (0)
Please sign in to comment.