// 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. SVNKit学习——使用High-Level API管理Working Copy示例(六)

    本篇内容是基于SVNKit High-Level API实现的针对Working copy的操作,操作内容与SVN图形化界面.命令行类似. High-Level API类图: 核心思想: 所有操作由各 ...

  2. 狂欢圣诞节,Azure云邀你一起云端跑酷!

    想要速度更快? 希望绕过障碍? 还想安全抵达目的地? …… 平安夜夜游时的各种畅想,不也正是 IT 同事所追求的,更是业务发展的终极目标呀. 好啦,小编说的太多了,这样不好不好.过节休息不谈工作,那就 ...

  3. [Err] 1214 - The used table type doesn't support FULLTEXT indexes

    -- -- Table structure for table `film_text` -- -- InnoDB added FULLTEXT support in 5.6.10. If you us ...

  4. 【Leetcode】【Easy】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. adb工具包使用方法

    ADB工具包总共有四个文件,两个exe后缀,两个dll后缀.里面还带有fastboot.exe下载后在PC上安装,如安装到D:\adb_tools-2.0目录,确认目录中带有fastboot.exe文 ...

  6. Wi-Fi

    AP就是一个无线的交换机,提供无线信号发射接收的功能 Wi-Fi是一种可以将个人电脑.手持设备(如PDA.手机)等终端以无线方式互相连接的技术 两个不一样的东西,无法比较的 你说的应该是无线路由器和无 ...

  7. Excel-怎样实现行列转置

    有时候,我们为了某些需要,必须把工作表的行列进行转置的方式显示.重新输入很浪费时间,怎样简单的实现转置呢,强大的excel2007提供了此项功能,具体怎么做,下面看我来演示一下. 工具/原料   装有 ...

  8. win8中 用office 提示值不在预期的范围内

    原文:http://bbs.kafan.cn/thread-1401951-1-1.htmlhttp://bbs.kafan.cn/thread-1401951-1-1.html 错误如下: 名称:  ...

  9. escape,unescape与encodeURIComponent,decodeURIComponent

    escape:将string转成unicode字符串 escape('

  10. 【[SCOI2009]粉刷匠】

    这好像是个暴力? 但是跑的挺快的 我们设\(dp[i][j][k]\)表示在第\(i\)行我们最远染到的位置是\(j\),这一行上一共染了\(k\)次最多能染对多少个格子 理性分析一下啊,每一行最多也 ...