LeetCode 675. Cut Off Trees for Golf Event
原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/
题目:
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:
0represents theobstaclecan't be reached.1represents thegroundcan be walked through.The place with number bigger than 1represents atreecan be walked through, and this positive number represents the tree's height.
You are asked to cut off all the trees in this forest in the order of tree's height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).
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. If you can't cut off all the trees, output -1 in that situation.
You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off.
Example 1:
Input:
[
[1,2,3],
[0,0,4],
[7,6,5]
]
Output: 6
Example 2:
Input:
[
[1,2,3],
[0,0,0],
[7,6,5]
]
Output: -1
Example 3:
Input:
[
[2,3,4],
[0,0,5],
[8,7,6]
]
Output: 6
Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.
题解:
用minHeap把树连着坐标高度都保存起来. poll出lowest tree, 用BFS算出起点坐标到lowest tree坐标距离加入res中.
Time Complexity: O(m^2 * n^2). m = forest.size(). n = forest.get(0).size(). 最多有m*n棵树, 每个树poll出来后BFS用时O(m*n).
Space: O(m^n). minHeap, que size.
AC Java:
class Solution {
int [][] dirs = {{0,1},{0,-1},{1,0},{-1,0}};
public int cutOffTree(List<List<Integer>> forest) {
if(forest == null || forest.size() == 0 || forest.get(0).size() == 0){
return 0;
}
int m = forest.size();
int n = forest.get(0).size();
PriorityQueue<int []> minHeap = new PriorityQueue<int []>((a, b) -> a[2] - b[2]);
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(forest.get(i).get(j) > 1){ // error
minHeap.add(new int[]{i, j, forest.get(i).get(j)});
}
}
}
int [] start = new int[2];
int res = 0;
while(!minHeap.isEmpty()){
int [] lowest = minHeap.poll();
int step = minStep(forest, start, lowest, m, n);
if(step < 0){
return -1;
}
res += step;
start[0] = lowest[0];
start[1] = lowest[1];
}
return res;
}
private int minStep(List<List<Integer>> forest, int [] start, int [] lowest, int m, int n){
int step = 0;
LinkedList<int []> que = new LinkedList<int []>();
boolean [][] used = new boolean[m][n];
que.add(start);
used[start[0]][start[1]] = true;
while(!que.isEmpty()){
int size = que.size();
for(int i = 0; i<size; i++){
int [] cur = que.poll();
if(cur[0] == lowest[0] && cur[1] == lowest[1]){
return step;
}
for(int [] dir : dirs){
int nx = cur[0] + dir[0];
int ny = cur[1] + dir[1];
if(nx<0 || nx>=m || ny<0 || ny>=n || used[nx][ny] || forest.get(nx).get(ny)==0){
continue;
}
que.add(new int[]{nx, ny});
used[nx][ny] = true;
}
}
step++;
}
return -1;
}
}
LeetCode 675. Cut Off Trees for Golf Event的更多相关文章
- [LeetCode] 675. Cut Off Trees for Golf Event 为高尔夫赛事砍树
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- [LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- 675. Cut Off Trees for Golf Event
// Potential improvements: // 1. we can use vector<int> { h, x, y } to replace Element, sortin ...
- [LeetCode] Cut Off Trees for Golf Event 为高尔夫赛事砍树
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- LeetCode - Cut Off Trees for Golf Event
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- [Swift]LeetCode675. 为高尔夫比赛砍树 | Cut Off Trees for Golf Event
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- 数据库原理及应用-用户接口及SQL查询语言(Query Language)
2018-02-07 20:41:39 一.DBMS的用户接口 查询语言 访问DBMS的访问工具(GUI) API 相关类库 二.SQL语言 SQL语言可以细分为四种: 1.Data Definiti ...
- UVALive-5135 Mining Your Own Business (无向图的双连通分量)
题目分析:在一张无向图中,将一些点涂上黑色,使得删掉图中任何一个点时,每个连通分量至少有一个黑点.问最少能涂几个黑点,并且在涂最少的情况下有几种方案. 题目分析:显然,一定不能涂割点.对于每一个连通分 ...
- HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)
题意 在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的. 思路 从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的. 倍长原序列, ...
- 使用 C++ 多态时需要注意的问题
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/ 最近工作中遇到一些关于使用多态的细节问题,在此记录一下. 一.多态和模板匹配 模板是 C+ ...
- element UI 中DateTimePicker 回传时间选择
之前在项目中用vue和element,日期和时间选择用的element2.0 的DateTimePicker 日期选择后提交没问题,在编辑页面通过后端返回时间字符串(敲黑板,这里是重点)绑定也没洒问题 ...
- [JS]JavaScript判断操作系统版本
function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == " ...
- 【51nod-1010】因子只含有2 3 5的数
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- LNMP的搭建与原理
常见的web框架结构:例如:lnmp和:ampL=LINUX N=NGINX A=APACHE P=php T=Tomcat lnmp的原理 在LNMP组合工作时,首先是用户通过浏览器输入域名请求Ng ...
- LeetCode OJ:LRU Cache(最近使用缓存)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- SpringXML方式配置bean的生命周期lifecycle
在Spring中容器在初始化某个bean的时候会有相应的生命周期,类似于Servlet,有相应的init,destory等方法 例如:如下service 1 2 3 4 5 6 7 8 9 10 11 ...