Skip to content

Commit c6b98a1

Browse files
add 675
1 parent 2ca644d commit c6b98a1

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727
|678|[Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_678.java) | O(n) | O(1) | Medium| Recursion, Greedy
2828
|677|[Map Sum Pairs](https://leetcode.com/problems/map-sum-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_677.java) | O(n) | O(n) | Medium | HashMap
2929
|676|[Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_676.java) | O(n^2) | O(n) | Medium |
30+
|675|[Cut Off Trees for Golf Event](https://leetcode.com/problems/cut-off-trees-for-golf-event/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_675.java) | O((m*n)^2) | O(m*n) | Hard | BFS
3031
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_674.java) | O(n^2) | O(1) | Easy |
3132
|672|[Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_672.java) | O(1) | O(1) | Medium | Math
3233
|671|[Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_671.java) | O(n) | O(n) | Easy | Tree, DFS
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.PriorityQueue;
6+
import java.util.Queue;
7+
8+
/**
9+
* 675. Cut Off Trees for Golf Event
10+
*
11+
* You are asked to cut off trees in a forest for a golf event.
12+
* The forest is represented as a non-negative 2D map, in this map:
13+
* 0 represents the obstacle can't be reached.
14+
* 1 represents the ground can be walked through.
15+
*
16+
* The place with number bigger than 1 represents a tree can be walked through,
17+
* and this positive number represents the tree's height.
18+
*
19+
* You are asked to cut off all the trees in this forest in the order of tree's height -
20+
* always cut off the tree with lowest height first.
21+
* And after cutting, the original place has the tree will become a grass (value 1).
22+
*
23+
* You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees.
24+
*
25+
* If you can't cut off all the trees, output -1 in that situation.
26+
* You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off.
27+
*
28+
* Example 1:
29+
* Input:
30+
* [
31+
* [1,2,3],
32+
* [0,0,4],
33+
* [7,6,5]
34+
* ]
35+
* Output: 6
36+
*
37+
* Example 2:
38+
* Input:
39+
* [
40+
* [1,2,3],
41+
* [0,0,0],
42+
* [7,6,5]
43+
* ]
44+
* Output: -1
45+
*
46+
* Example 3:
47+
* Input:
48+
* [
49+
* [2,3,4],
50+
* [0,0,5],
51+
* [8,7,6]
52+
* ]
53+
* Output: 6
54+
*
55+
* Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.
56+
* Hint: size of the given matrix will not exceed 50x50.
57+
*/
58+
59+
public class _675 {
60+
public static class Solution1 {
61+
public int cutOffTree(List<List<Integer>> forest) {
62+
if (forest == null || forest.isEmpty() || forest.size() == 0 || forest.get(0).get(0) == 0) {
63+
return -1;
64+
}
65+
int m = forest.size();
66+
int n = forest.get(0).size();
67+
/**cut trees in ascending order*/
68+
PriorityQueue<Tree> heap = new PriorityQueue<>((a, b) -> a.height - b.height);
69+
for (int i = 0; i < m; i++) {
70+
for (int j = 0; j < n; j++) {
71+
if (forest.get(i).get(j) > 1) {
72+
/**This is important: we'll add trees that are only taller than 1!!!*/
73+
heap.offer(new Tree(i, j, forest.get(i).get(j)));
74+
}
75+
}
76+
}
77+
78+
int sum = 0;
79+
Tree start = new Tree();
80+
while (!heap.isEmpty()) {
81+
Tree curr = heap.poll();
82+
int step = bfs(forest, curr, start, m, n);
83+
if (step == -1) {
84+
return -1;
85+
}
86+
sum += step;
87+
start = curr;
88+
}
89+
return sum;
90+
}
91+
92+
private int bfs(List<List<Integer>> forest, Tree target, Tree start, int m, int n) {
93+
int[] dirs = new int[]{0, 1, 0, -1, 0};
94+
boolean[][] visited = new boolean[m][n];
95+
Queue<Tree> queue = new LinkedList<>();
96+
queue.offer(start);
97+
visited[start.x][start.y] = true;
98+
int step = 0;
99+
while (!queue.isEmpty()) {
100+
int size = queue.size();
101+
for (int k = 0; k < size; k++) {
102+
Tree tree = queue.poll();
103+
if (tree.x == target.x && tree.y == target.y) {
104+
return step;
105+
}
106+
107+
for (int i = 0; i < 4; i++) {
108+
int nextX = tree.x + dirs[i];
109+
int nextY = tree.y + dirs[i + 1];
110+
if (nextX < 0 || nextY < 0 || nextX >= m || nextY >= n || visited[nextX][nextY] || forest.get(nextX).get(nextY) == 0) {
111+
continue;
112+
}
113+
queue.offer(new Tree(nextX, nextY, forest.get(nextX).get(nextY)));
114+
visited[nextX][nextY] = true;
115+
}
116+
}
117+
step++;
118+
}
119+
return -1;
120+
}
121+
122+
class Tree {
123+
int x;
124+
int y;
125+
int height;
126+
127+
public Tree(int x, int y, int height) {
128+
this.x = x;
129+
this.y = y;
130+
this.height = height;
131+
}
132+
133+
public Tree() {
134+
this.x = 0;
135+
this.y = 0;
136+
this.height = 0;
137+
}
138+
}
139+
}
140+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.ArrayUtils;
4+
import com.fishercoder.solutions._675;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class _675Test {
15+
private static _675.Solution1 solution1;
16+
private static List<List<Integer>> forest;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _675.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
forest = new ArrayList<>();
26+
forest.add(Arrays.asList(1, 2, 3));
27+
forest.add(Arrays.asList(0, 0, 4));
28+
forest.add(Arrays.asList(7, 6, 5));
29+
assertEquals(6, solution1.cutOffTree(forest));
30+
}
31+
32+
@Test
33+
public void test2() {
34+
forest = new ArrayList<>();
35+
forest.add(Arrays.asList(1, 2, 3));
36+
forest.add(Arrays.asList(0, 0, 0));
37+
forest.add(Arrays.asList(7, 6, 5));
38+
assertEquals(-1, solution1.cutOffTree(forest));
39+
}
40+
41+
@Test
42+
public void test3() {
43+
forest = new ArrayList<>();
44+
forest.add(Arrays.asList(2, 3, 4));
45+
forest.add(Arrays.asList(0, 0, 5));
46+
forest.add(Arrays.asList(8, 7, 6));
47+
assertEquals(6, solution1.cutOffTree(forest));
48+
}
49+
50+
@Test
51+
public void test4() {
52+
forest = ArrayUtils.buildList(new int[][]{
53+
{2, 3, 4, 9},
54+
{0, 0, 5, 10},
55+
{8, 7, 6, 12},
56+
});
57+
assertEquals(13, solution1.cutOffTree(forest));
58+
}
59+
60+
@Test
61+
public void test5() {
62+
forest = ArrayUtils.buildList(new int[][]{
63+
{0, 0, 0, 3528, 2256, 9394, 3153},
64+
{8740, 1758, 6319, 3400, 4502, 7475, 6812},
65+
{0, 0, 3079, 6312, 0, 0, 0},
66+
{6828, 0, 0, 0, 0, 0, 8145},
67+
{6964, 4631, 0, 0, 0, 4811, 0},
68+
{0, 0, 0, 0, 9734, 4696, 4246},
69+
{3413, 8887, 0, 4766, 0, 0, 0},
70+
{7739, 0, 0, 2920, 0, 5321, 2250},
71+
{3032, 0, 3015, 0, 3269, 8582, 0}});
72+
assertEquals(-1, solution1.cutOffTree(forest));
73+
}
74+
75+
@Test
76+
public void test6() {
77+
forest = ArrayUtils.buildList(new int[][]{
78+
{54581641, 64080174, 24346381, 69107959},
79+
{86374198, 61363882, 68783324, 79706116},
80+
{668150, 92178815, 89819108, 94701471},
81+
{83920491, 22724204, 46281641, 47531096},
82+
{89078499, 18904913, 25462145, 60813308}
83+
});
84+
assertEquals(57, solution1.cutOffTree(forest));
85+
}
86+
87+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy