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:
0
represents theobstacle
can't be reached.1
represents theground
can be walked through.The place with number bigger than 1
represents atree
can 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 ...
随机推荐
- winform无边框窗体点击任务栏最小化
protected override CreateParams CreateParams { get { const int WS_MINIMIZEBOX = 0x00020000; // Winus ...
- CentOS开端口问题
关闭SELINUX ##查看SELINUX状态 /usr/sbin/sestatus -v getenforce #修改config配置文件,重启后即可 vi /etc/selinux/config ...
- rsa加解密密钥生成命令
(1)生成原始RSA私钥文件 rsa_private_key.pem(原始私钥) openssl genrsa -out rsa_private_key.pem 2048 (2)将原始RSA私钥转换为 ...
- Ansible 开发调试 之【pycharm远程调试】
介绍 PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本 ...
- 华为EPON OLT开局配置
配置思路: 1. 登录olt(console进去之后配地址) 2.配置上联口(配vlan和起三层地址互联路由的lan口) 3.epon接分光器,分光器下接光猫 4.自动发现光猫.配置DBA数据和线 ...
- shiro权限定义的三种方法
1.在配置文件中定义 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFa ...
- WebApplication和WebSite的简单区别
初步认识网站和网站应用程序区别 1. 网页头部文件 网站 <%@ Page Language="VB" AutoEventWireup="false" C ...
- bzoj2547
题解: 二分+宽搜+KM 显然答案不能太大 然后二分一下 代码: #include<bits/stdc++.h> ,M=; using namespace std; ]={-,,,},dy ...
- 《Effective C++》第2章 构造/析构/赋值运算(1)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- Flask项目中的蓝图简介及使用方式
Blueprint概念 简单来说,Blueprint 是一个存储操作方法的容器,这些操作在这个Blueprint 被注册到一个应用之后就可以被调用,Flask 可以通过Blueprint来组织URL以 ...