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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- maven学习(五)插件和自定义插件
插件是可以配置在settings.xml和pom.xml中的 插件目标: 在了解插件和生命周期的绑定关系之前,先来说一下插件目标.在实际项目构建的过程中,需要经历编译.打包等等许许多多的操作,为每个操 ...
- 【量产工具修复】U盘插上没反应,格式化提示有写保护
最近在实验室发现师兄留下的U盘,插上电脑后打不开,弹出格式化界面,格式化的时候又提示该u盘“被写保护无法格式化”,于是打算采用量产的方法. 第一步:使用chipgenius监测u盘的芯片制造商和型号 ...
- 增强for循环 java.util.ConcurrentModificationException
Java中的Iterator功能比较简单,并且只能单向移动: (1) 使用方法iterator()要求容器返回一个Iterator.第一次调用Iterator的next()方法时,它返回序列的第一个元 ...
- March 17 2017 Week 11 Friday
Simplicity is the ultimate sophistication. 简约才是精巧到了极致. Recently I have spent a great number of time ...
- 第二次scrum冲击
1.小组第二次冲刺任务及其完成情况描述. 本次冲刺我们小组经过讨论,实现的使我们爱上长大系统中的失物招领功能,由于在实际的实现中,对于本功能的逐渐深入和了解,渐渐发现这个功能实现起来需要由很多部分组成 ...
- HDU 6214 最小割边
双倍经验题:HDU 6214,3987 求最小割的最小边. 方案一: 首先跑最大流,这个时候割上都满载了,于是将满载的边 cap = 1,其他 inf ,再跑最大流,这个时候限定这个网络的关键边就是那 ...
- @WebListener 注解方式实现监听(eclipse和idea)
eclipse进行演示: 1.创建 Dynamic Web Project ,Dynamic Web module version选择3.0 2.在自动生成 的web.xml配置,增加 metadat ...
- Oracle数据库大量library cache: mutex X及latch: shared pool问题排查一例
业务系统数据库夯住,数据库内大量的library cache: mutex X及latch: shared pool等待,alert日志信息如下 Tue Sep :: WARNING: inbound ...
- 分布式id生成
2016年08月09日 14:15:21 yuanyuanispeak 阅读数:318 编辑 一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id ...
- flexible.js在华某为手机上使用rem时,页面宽度超出手机屏幕宽度
问题:手机端项目在华为的某款手机上显示时页面内容没有自适应手机宽度,出现横向滚动条 原因:手机获取手机屏幕宽度并计算出rem时出现偏差,明显宽余真实手机屏宽度 解决方案一:在页面里获取页面最外层dom ...