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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 删除排序数组中的重复数字 - C++
class Solution { public: /** * @param A: a list of integers * @return : return an integer */ int rem ...
- redis的使用方式
常用的语法以及使用方式: key中不能包含回车空格等,key不要太长,占用内存. 概念介绍: 差集: a:{1,2,3} b:{2,3,4},以a为锚点,差集 ...
- 【SQL Server 2012】按倒序存储“分组统计”结果的临时表到新建表
程序预先说明: 本文访问的数据库是基于存有RDF三元组的开源数据库Localyago修改的库,其中只有一个表,表中有五个属性:主语subject.谓语predict.宾语object.主语的编号sub ...
- 常见协议TCP、UDP、IP图
ip tcp udp icmp help ip tcp http icmp
- Windows 系统 Unicode 文件名操作(新建、重命名、枚举、复制)全攻略
常见的那些文件操作函数都不支持,于是为了达到目的,需要各种方法配合,应该是不如其他语言方便.我只是想看看Perl到底是否适合做这件事,于是折腾了一回. 文件的建立: 模块:Win32 Code: [全 ...
- 解决svn中“工作副本已经锁定”,或者svn清理失败的解决方法
刚开始遇到这个问题还以为是没有插网线的原因,客户端和服务器都在我的电脑上,但是更新和提交都执行不了,以为是没有插网线就没把这个小问题放在心上,今早上还是这样,就不得不解决一下了. 更新或者提交前要执行 ...
- About me and this site(2018/8/28)
Name: 李秋豪 / [lǐ qiū háo] I'm a junior majoring in InfoSec at Harbin Institute of Technology. I'm int ...
- 简单使用Idea创建三层架构项目和数据库连接(使用原生ajax进行访问+ajax)
Idea创建三层架构项目 首先创建一个Web项目model 创建Web完成后进行创建entity.dao.service 特别注意 根据上面的步骤进行创建即可得到 创建完成 我们首先创建数据库 cre ...
- 【洛谷P3818】小A和uim之大逃离 II
小A和uim之大逃离 II 题目链接 比较裸的搜索,vis[i][j]再加一层[0/1]表示是否使用过魔液 转移时也将是否使用过魔液记录下来,广搜即可 #include<iostream> ...
- GOPL第三章练习题3.3 3.4代码
练习3.3是peak展示为红色,valley展示为蓝色. 练习3.4是将svg图像打印到浏览器中. // Copyright © 2016 Alan A. A. Donovan & Brian ...