Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

675. Cut Off Trees for Golf Event

Hard34.8% Acceptance
ArrayBreadth-First SearchHeap (Priority Queue)
Asked by:
Amazon
ProblemSolutions (9)VideosCompanies (2)Notes

Problem Statement

You are asked to cut off all the trees in a forest for a golf event. The forest is represented as an m x n matrix. In this matrix:

  • 0 means the cell cannot be walked through.
  • 1 represents an empty cell that can be walked through.
  • A number greater than 1 represents a tree in a cell that can be walked through, and this number is the tree's height.

In one step, you can walk in any of the four directions: north, east, south, and west. If you are standing in a cell with a tree, you can choose whether to cut it off.

You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value at its cell becomes 1 (an empty cell).

Starting from the point (0, 0), return the minimum steps you need to walk to cut off all the trees. If you cannot cut off all the trees, return -1.

Note: The input is generated such that no two trees have the same height, and there is at least one tree needs to be cut off.

Example 1:

Input: forest = [[1,2,3],[0,0,4],[7,6,5]]
Output: 6
Explanation: Following the path above allows you to cut off the trees from shortest to tallest in 6 steps.

Example 2:

Input: forest = [[1,2,3],[0,0,0],[7,6,5]]
Output: -1
Explanation: The trees in the bottom row cannot be accessed as the middle row is blocked.

Example 3:

Input: forest = [[2,3,4],[0,0,5],[8,7,6]]
Output: 6
Explanation: You can follow the same path as Example 1 to cut off all the trees.
Note that you can cut off the first tree at (0, 0) before making any steps.

Constraints:

  • m == forest.length
  • n == forest[i].length
  • 1 <= m, n <= 50
  • 0 <= forest[i][j] <= 109
  • Heights of all trees are distinct.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
A
Apple

Approach

In #675 Cut Off Trees for Golf Event, the forest is represented as a matrix where each cell may contain a tree with a certain height. The objective is to cut all trees in increasing order of height while minimizing the total steps taken. A common strategy is to first collect all trees and sort them by height (often using a list or priority queue). Starting from position (0,0), you then repeatedly move to the next tree in sorted order.

To compute the shortest path between the current position and the next tree, apply Breadth-First Search (BFS) on the grid. BFS works well because each move has equal cost and the grid may contain obstacles (cells with value 0). If any tree cannot be reached, the result is -1. This approach essentially performs multiple shortest-path searches across the matrix while processing trees in sorted order.

The overall efficiency depends on the number of trees and repeated BFS traversals across the grid.

Complexity

ApproachTime ComplexitySpace Complexity
Sort Trees + BFS for Shortest PathO(T * m * n)O(m * n)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (9)

Breadth-First Search (BFS) for Shortest Path

This approach involves using a BFS algorithm to find the shortest path between the starting point and each tree in increasing order of their heights. We first list all trees with their positions and heights, sort them, and for each sorted tree, compute the shortest path using BFS. If any tree is unreachable, we return -1.

The time complexity of this approach is O(T * m * n) where T is the number of trees. This is because we perform BFS from the starting position to each tree. The space complexity is O(m * n) due to the storage of the queue and visited matrix in BFS.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdbool.h>
4#include <limits.h>
5
6// Define directions for moving in the grid

    
    


Explanation

This C solution first uses BFS to calculate the shortest path between trees, starting from (0, 0) and moving to trees in increasing order of their height. The binary search is used to sort the trees based on their height first. If any tree is unreachable during a BFS computation, the function returns -1.

Bidirectional BFS for Optimal Path Finding

Using Bidirectional BFS can help in optimizing the pathfinding step by searching from both the start and the goal concurrently. The aim is to decrease the search space substantially as the two searches meet in the middle. This method is an extension to the regular BFS approach, potentially optimizing the traversal cost considerably under specific configurations.

The potential benefits of bidirectional search in space complexity can be realized when effectively reducing the number of explored nodes, as time complexity ideally shortens from O(b^d) to roughly O(b^(d/2)), where b is the branching factor and d the solution depth.

CC++Python
1Implementing a complete bidirectional BFS in C is complex and often involves intricate management of forward and backward search queues and visited states. This might require substantial utility function support for

Video Solutions

Watch expert explanations and walkthroughs

Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python

NeetCodeIO
12:2522,052 views

Asked By Companies

2 companies
A
Amazon
A
Apple

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Surrounded RegionsMedium
Number of IslandsMedium
Alien DictionaryHard
Walls and GatesMedium
More similar problems

Related Topics

ArrayBreadth-First SearchHeap (Priority Queue)Matrix

Problem Stats

Acceptance Rate34.8%
DifficultyHard
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why is BFS used in Cut Off Trees for Golf Event?

BFS guarantees the shortest path in an unweighted grid where each move has equal cost. Since the player can move in four directions and obstacles exist, BFS helps determine the minimum number of steps between trees.

Is Cut Off Trees for Golf Event asked in FAANG interviews?

Yes, this problem is considered a challenging grid traversal question and can appear in interviews at companies like Google, Amazon, or Meta. It tests BFS, matrix traversal, and the ability to combine sorting with repeated shortest-path searches.

What data structure is best for Cut Off Trees for Golf Event?

A list or priority queue is useful for sorting trees by height, while a queue is used for BFS traversal of the grid. BFS is ideal because it finds the shortest path in an unweighted grid efficiently.

What is the optimal approach for Cut Off Trees for Golf Event?

The optimal approach is to first collect all trees and sort them by height. Then repeatedly run BFS from the current position to the next tree in sorted order to find the shortest path. If any tree is unreachable, return -1.

7
int
directions
[
4
]
[
2
]
=
{
{
-
1
,
0
}
,
{
1
,
0
}
,
{
0
,
-
1
}
,
{
0
,
1
}
}
;
8
9
// Function prototypes
10
int
bfs
(
int
*
*
forest
,
int
m
,
int
n
,
int
sr
,
int
sc
,
int
tr
,
int
tc
)
;
11
int
cmpfunc
(
const
void
*
a
,
const
void
*
b
)
;
12
13
int
cutOffTree
(
int
*
*
forest
,
int
forestSize
,
int
*
forestColSize
)
{
14
int
m
=
forestSize
;
15
int
n
=
forestColSize
[
0
]
;
16
17
// Extract trees and sort by height
18
int
trees
[
m
*
n
]
[
3
]
,
count
=
0
;
19
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
20
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
21
if
(
forest
[
i
]
[
j
]
>
1
)
{
22
trees
[
count
]
[
0
]
=
forest
[
i
]
[
j
]
;
23
trees
[
count
]
[
1
]
=
i
;
24
trees
[
count
]
[
2
]
=
j
;
25
count
++
;
26
}
27
}
28
}
29
qsort
(
trees
,
count
,
sizeof
(
trees
[
0
]
)
,
cmpfunc
)
;
30
31
// Start cutting trees
32
int
totalSteps
=
0
,
sr
=
0
,
sc
=
0
;
33
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
34
int
tr
=
trees
[
i
]
[
1
]
;
35
int
tc
=
trees
[
i
]
[
2
]
;
36
int
steps
=
bfs
(
forest
,
m
,
n
,
sr
,
sc
,
tr
,
tc
)
;
37
if
(
steps
==
-
1
)
return
-
1
;
38
totalSteps
+=
steps
;
39
sr
=
tr
;
40
sc
=
tc
;
41
}
42
return
totalSteps
;
43
}
44
45
int
bfs
(
int
*
*
forest
,
int
m
,
int
n
,
int
sr
,
int
sc
,
int
tr
,
int
tc
)
{
46
if
(
sr
==
tr
&&
sc
==
tc
)
return
0
;
47
bool visited
[
m
]
[
n
]
;
48
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
49
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
50
visited
[
i
]
[
j
]
=
false
;
51
}
52
}
53
visited
[
sr
]
[
sc
]
=
true
;
54
int
queue
[
m
*
n
]
[
2
]
;
55
int
front
=
0
,
back
=
0
;
56
queue
[
back
]
[
0
]
=
sr
;
57
queue
[
back
++
]
[
1
]
=
sc
;
58
int
steps
=
0
;
59
while
(
front
<
back
)
{
60
int
size
=
back
-
front
;
61
steps
++
;
62
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
63
int
r
=
queue
[
front
]
[
0
]
;
64
int
c
=
queue
[
front
]
[
1
]
;
65
front
++
;
66
for
(
int
d
=
0
;
d
<
4
;
d
++
)
{
67
int
nr
=
r
+
directions
[
d
]
[
0
]
;
68
int
nc
=
c
+
directions
[
d
]
[
1
]
;
69
if
(
nr
>=
0
&&
nr
<
m
&&
nc
>=
0
&&
nc
<
n
&&
!
visited
[
nr
]
[
nc
]
&&
forest
[
nr
]
[
nc
]
>
0
)
{
70
if
(
nr
==
tr
&&
nc
==
tc
)
return
steps
;
71
visited
[
nr
]
[
nc
]
=
true
;
72
queue
[
back
]
[
0
]
=
nr
;
73
queue
[
back
++
]
[
1
]
=
nc
;
74
}
75
}
76
}
77
}
78
return
-
1
;
79
}
80
81
int
cmpfunc
(
const
void
*
a
,
const
void
*
b
)
{
82
int
l
=
(
(
int
*
)
a
)
[
0
]
;
83
int
r
=
(
(
int
*
)
b
)
[
0
]
;
84
return
(
l
-
r
)
;
85
}
handling interaction between the dual BFS processes
,
often involving custom management of states
.

Explanation

A detailed explanation of handling bidirectional BFS in C would involve structuring two queues for the forward and backward search, ensuring efficient state management overlaps and transitions by interfacing node positions from both directions dynamically, essentially shortening the BFS travel path.