675. Cut Off Trees for Golf Event
// Potential improvements:
// 1. we can use vector<int> { h, x, y } to replace Element, sorting vector is to compare elements one by one.
// 2. use 2-d bool vector<vector<bool>> to replace unordered_set. class Element {
public:
int x, y, h;
Element(int x, int y, int h) {
this->x = x;
this->y = y;
this->h = h;
}
};
class Solution {
public:
int m;
int n;
int cutOffTree(vector<vector<int>>& forest) {
m = forest.size(); if (m == ) return ;
n = forest[].size(); if (n == ) return ;
vector<Element> v;
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (forest[i][j] > )
v.emplace_back(i, j, forest[i][j]);
auto comp = [](const Element& a, const Element& b) { return a.h < b.h; };
v.emplace_back(, , );
sort(v.begin(), v.end(), comp); int res = ;
for (int i = ; i < v.size() - ; i++) {
int t = helper(forest, v[i], v[i+]);
if (t < ) return t;
res += t;
}
return res;
}
int helper(vector<vector<int>>& forest, const Element& a, const Element& b) {
const int dirs[] = { -, , , , - };
// (x,y) is small enough, so x*n+y won't overflow. otherwise we have to use long,
// and be careful x*n+y will overflow, we may use (long)x*n+y instead.
unordered_set<int> s;
queue<pair<int,int>> q;
q.push({a.x, a.y});
s.insert(a.x * n + a.y);
int lv = ;
while (!q.empty()) {
int qsz = q.size();
for (int i = ; i < qsz; i++) {
auto cur = q.front();
q.pop();
if (cur.first == b.x && cur.second == b.y)
return lv;
for (int i = ; i < ; i++) {
int nx = cur.first + dirs[i];
int ny = cur.second + dirs[i+];
pair<int,int> np = {nx,ny};
if (nx >= && nx < m && ny >= && ny < n &&
forest[nx][ny] > &&
s.find(nx * n + ny) == s.end()) {
q.push(np);
s.insert(nx * n + ny);
}
}
}
lv++;
}
return -;
}
};
huge perf improve from 1000+ ms to 300 ms:
use 2-d bool vector<vector<bool>> to replace unordered_set
class Element {
public:
int x, y, h;
Element(int x, int y, int h) {
this->x = x;
this->y = y;
this->h = h;
}
};
class Solution {
public:
int m;
int n;
int cutOffTree(vector<vector<int>>& forest) {
m = forest.size(); if (m == ) return ;
n = forest[].size(); if (n == ) return ;
vector<Element> v;
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (forest[i][j] > )
v.emplace_back(i, j, forest[i][j]);
auto comp = [](const Element& a, const Element& b) { return a.h < b.h; };
v.emplace_back(, , );
sort(v.begin(), v.end(), comp); int res = ;
for (int i = ; i < v.size() - ; i++) {
int t = helper(forest, v[i], v[i+]);
if (t < ) return t;
res += t;
}
return res;
}
int helper(vector<vector<int>>& forest, const Element& a, const Element& b) {
const int dirs[] = { -, , , , - };
// (x,y) is small enough, so x*n+y won't overflow. otherwise we have to use long,
// and be careful x*n+y will overflow, we may use (long)x*n+y instead.
vector<vector<bool>> s(m, vector<bool>(n));
queue<pair<int,int>> q;
q.emplace(a.x, a.y);
s[a.x][a.y] = true;
int lv = ;
while (!q.empty()) {
int qsz = q.size();
for (int i = ; i < qsz; i++) {
auto cur = q.front();
q.pop();
if (cur.first == b.x && cur.second == b.y)
return lv;
for (int i = ; i < ; i++) {
int nx = cur.first + dirs[i];
int ny = cur.second + dirs[i+];
pair<int,int> np = {nx,ny};
if (nx >= && nx < m && ny >= && ny < n &&
forest[nx][ny] > &&
!s[nx][ny]) {
q.push(np);
s[nx][ny] = true;
}
}
}
lv++;
}
return -;
}
};
675. Cut Off Trees for Golf Event的更多相关文章
- LeetCode 675. Cut Off Trees for Golf Event
原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/description/ 题目: You are asked to ...
- [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] 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 - 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 ...
- Christmas Trees, Promises和Event Emitters
今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- WebLogic配置与部署
一.创建域: 第一步,选择“开始菜单”-> “Oracle WebLogic”-> “WebLogic Server 10gR3” -> “Tools”-> “Configur ...
- CSS中的EM属性之弹性布局
这篇教程将引导大家如何使用“em”来创建一个基本的弹性布局,从而学习其如何计算?又是如何使用“em”对层进行弹性扩展?又是如何扩展文本和图像等内容?下在我们就一起带着这些问题开始今天的“em”之行. ...
- Bootstrap table分页问题汇总
首先非常感谢作者针对bootstrap table分页问题进行详细的整理,并分享给了大家,希望通过这篇文章可以帮助大家解决Bootstrap table分页的各种问题,谢谢大家的阅读. 问题1 :服务 ...
- Going deeper with convolutions(GoogLeNet、Inception)
从LeNet-5开始,cnn就有了标准的结构:stacked convolutional layers are followed by one or more fully-connected laye ...
- 调用URL 接口服务
1.Net调用URL 接口服务 using System; using System.Collections; using System.Configuration; using System.Dat ...
- CSU 1023 修路(二分+模拟)
前段时间,某省发生干旱,B山区的居民缺乏生活用水,现在需要从A城市修一条通往B山区的路.假设有A城市通往B山区的路由m条连续的路段组成,现在将这m条路段承包给n个工程队(n ≤ m ≤ 300).为了 ...
- linux 学习(二)防火墙
ubuntu 第四 防火墙 安装 sudo apt-get install ufw 启用 sudo ufw enable 拒绝所有 sudo default deny 开启端口 sudo ufw al ...
- 【oracle使用笔记2】使用Oracle数据库遇到的若干问题总结
一. 关于Oracle 11g数据库在查询表中数据显示中文乱码问题 [描述]本人一开始使用的Oracle是11g版本的,用PLSQL一次查询表中的数据时出现了中文显示乱码,为此搜了许多解决办法,最终通 ...
- Linux基础 ppt pptx
引言 以前写过一个讲 Linux 基础的ppt,琢磨着把它分享出来,有需要的请自取. 部分截图如下 下载地址 下载地址1
- [HNOI2003]操作系统(优先队列,堆排序)
题目描述 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高. 如果一个进程到达的时 ...