// 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的更多相关文章

  1. LeetCode 675. Cut Off Trees for Golf Event

    原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/description/ 题目: You are asked to ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. Christmas Trees, Promises和Event Emitters

    今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...

  8. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. Siebel Tools 开发笔记

    1.在Siebel Client上的菜单Help -> View 中可以找到开发所常用的信息 Screen:   此画面所使用的Screen名字在Tools的Object Explorer中的[ ...

  2. Flask博客类登录注册验证模块代码(十四)

    1 文件系统 blog #博客类 App forms #表单 __init__.py user.py models #模型 __init__.py user.py static #静态文件 templ ...

  3. sqlserver 带输出参数的存储过程

    --创建存储过程create procedure proc_stu@sname varchar(20),@pwd varchar(50),@flag bit outputasif exists(sel ...

  4. C++ decltype类型说明符(尾置返回类型使用)

    转自https://blog.csdn.net/yhl_leo/article/details/50865552 1 基本语法 decltype 类型说明符生成指定表达式的类型.在此过程中,编译器分析 ...

  5. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  6. js 原生获取Class元素

    function getElementsByClassName(n) { var classElements = [] allElements = document.getElementsByTagN ...

  7. JavaScript面向对象编程之创建对象

    参考资料依旧<JavaScript高级程序设计>,不得不说这本书写的太好了,讲的极为清晰凝练,好书! 先给出重点笔记,好好理解下面的三条笔记,每一句话都很重要: 1.实例的指针仅指向原型, ...

  8. 01_常用 Linux 命令的基本使用

    01. 学习 Linux 终端命令的原因 Linux 刚面世时并没有图形界面,所有的操作全靠命令完成,如 磁盘操作.文件存取.目录操作.进程管理.文件权限 设定等 在职场中,大量的 服务器维护工作 都 ...

  9. SqlSugar之SqlQueryDynamic返回值处理

    现在有个需求,有一张表每个月表名都会变的,但结构是一样的,我们不能再用类映射来完成的,我不能每个月都去手动添加,我们只能使用sql语句来完成这个需求.为了方便我这边选择的是SqlQueryDynami ...

  10. code First 四

    先从现有数据库获取代码:  我们创建模型的时候选择Code First就可以了 public ModelStudent() : base("name=ModelStudent") ...