Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc6b98a1

Browse files
add 675
1 parent2ca644d commitc6b98a1

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.LinkedList;
4+
importjava.util.List;
5+
importjava.util.PriorityQueue;
6+
importjava.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+
publicclass_675 {
60+
publicstaticclassSolution1 {
61+
publicintcutOffTree(List<List<Integer>>forest) {
62+
if (forest ==null ||forest.isEmpty() ||forest.size() ==0 ||forest.get(0).get(0) ==0) {
63+
return -1;
64+
}
65+
intm =forest.size();
66+
intn =forest.get(0).size();
67+
/**cut trees in ascending order*/
68+
PriorityQueue<Tree>heap =newPriorityQueue<>((a,b) ->a.height -b.height);
69+
for (inti =0;i <m;i++) {
70+
for (intj =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(newTree(i,j,forest.get(i).get(j)));
74+
}
75+
}
76+
}
77+
78+
intsum =0;
79+
Treestart =newTree();
80+
while (!heap.isEmpty()) {
81+
Treecurr =heap.poll();
82+
intstep =bfs(forest,curr,start,m,n);
83+
if (step == -1) {
84+
return -1;
85+
}
86+
sum +=step;
87+
start =curr;
88+
}
89+
returnsum;
90+
}
91+
92+
privateintbfs(List<List<Integer>>forest,Treetarget,Treestart,intm,intn) {
93+
int[]dirs =newint[]{0,1,0, -1,0};
94+
boolean[][]visited =newboolean[m][n];
95+
Queue<Tree>queue =newLinkedList<>();
96+
queue.offer(start);
97+
visited[start.x][start.y] =true;
98+
intstep =0;
99+
while (!queue.isEmpty()) {
100+
intsize =queue.size();
101+
for (intk =0;k <size;k++) {
102+
Treetree =queue.poll();
103+
if (tree.x ==target.x &&tree.y ==target.y) {
104+
returnstep;
105+
}
106+
107+
for (inti =0;i <4;i++) {
108+
intnextX =tree.x +dirs[i];
109+
intnextY =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(newTree(nextX,nextY,forest.get(nextX).get(nextY)));
114+
visited[nextX][nextY] =true;
115+
}
116+
}
117+
step++;
118+
}
119+
return -1;
120+
}
121+
122+
classTree {
123+
intx;
124+
inty;
125+
intheight;
126+
127+
publicTree(intx,inty,intheight) {
128+
this.x =x;
129+
this.y =y;
130+
this.height =height;
131+
}
132+
133+
publicTree() {
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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.ArrayUtils;
4+
importcom.fishercoder.solutions._675;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importjava.util.ArrayList;
9+
importjava.util.Arrays;
10+
importjava.util.List;
11+
12+
importstaticorg.junit.Assert.assertEquals;
13+
14+
publicclass_675Test {
15+
privatestatic_675.Solution1solution1;
16+
privatestaticList<List<Integer>>forest;
17+
18+
@BeforeClass
19+
publicstaticvoidsetup() {
20+
solution1 =new_675.Solution1();
21+
}
22+
23+
@Test
24+
publicvoidtest1() {
25+
forest =newArrayList<>();
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+
publicvoidtest2() {
34+
forest =newArrayList<>();
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+
publicvoidtest3() {
43+
forest =newArrayList<>();
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+
publicvoidtest4() {
52+
forest =ArrayUtils.buildList(newint[][]{
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+
publicvoidtest5() {
62+
forest =ArrayUtils.buildList(newint[][]{
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+
publicvoidtest6() {
77+
forest =ArrayUtils.buildList(newint[][]{
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)

[8]ページ先頭

©2009-2025 Movatter.jp