leetcode:the skyline problem
来源:互联网 发布:淘宝手机开店确认协议 编辑:程序博客网 时间:2024/05/16 04:46
class Solution{public: struct node { int x, heightIndex; bool leftOrRight; node(int _x, int _heightIndex, bool _leftOrRight):x(_x), heightIndex(_heightIndex), leftOrRight(_leftOrRight) {} }; static bool cmp(const node & a, const node & b)//去掉static报错 { if(a.x != b.x) return a.x < b.x;//小于号不能换成减号 return a.leftOrRight < b.leftOrRight; } vector<pair<int, int> > getSkyline(vector<vector<int> >& buildings) { vector<pair<int, int> > res; vector<node> edge; vector<int> height; int i, index; for(i = 0; i < buildings.size(); i++) { index = height.size(); edge.push_back(node(buildings[i][0], index, false)); edge.push_back(node(buildings[i][1], index, true)); height.push_back(buildings[i][2]); } sort(edge.begin(), edge.end(), cmp); multiset<int> active; int currentHeight = 0, newHeight, newX; for(i = 0; i < edge.size(); i++) { if(edge[i].leftOrRight == false) { active.insert(edge[i].heightIndex); newHeight = height[edge[i].heightIndex]; newX = edge[i].x; while(i + 1 < edge.size() && edge[i].x == edge[i + 1].x && edge[i + 1].leftOrRight == false) { active.insert(edge[i + 1].heightIndex); newHeight = max(newHeight, height[edge[i + 1].heightIndex]); i++; } if(currentHeight < newHeight) { res.push_back(make_pair(newX, newHeight)); currentHeight = newHeight; } } else { active.erase(edge[i].heightIndex); newHeight = height[edge[i].heightIndex]; newX = edge[i].x; while(i + 1 < edge.size() && edge[i].x == edge[i + 1].x && edge[i + 1].leftOrRight == true) { active.erase(edge[i + 1].heightIndex); newHeight = max(newHeight, height[edge[i + 1].heightIndex]); i++; } if(currentHeight == newHeight) { multiset<int> :: iterator it; int maxHeight = 0; for(it = active.begin(); it != active.end(); it++) maxHeight = max(maxHeight, height[*it]); if(newHeight > maxHeight) { res.push_back(make_pair(newX, maxHeight)); currentHeight = maxHeight; } } } } return res; }};
0 0
- leetcode - The Skyline Problem
- LeetCode - The Skyline Problem
- leetcode:the skyline problem
- The Skyline Problem -- leetcode
- Leetcode -- The Skyline Problem
- leetcode 218: The Skyline Problem
- Leetcode 218 The Skyline Problem
- The Skyline Problem Leetcode Java
- leetcode 218: The Skyline Problem
- Leetcode #218 The Skyline Problem
- [leetcode] 218.The Skyline Problem
- [LeetCode 218] The Skyline Problem
- LeetCode 218. The Skyline Problem
- LeetCode 218. The Skyline Problem
- [LeetCode]--218. The Skyline Problem
- [LeetCode]218. The Skyline Problem
- 218. The Skyline Problem [leetcode]
- leetcode 218. The Skyline Problem
- excel导入MySQL 学习
- li:hover{ color: #ffffff; }
- 神经网络-并行BP算法
- java中使用日志组件和mysql数据库
- 平面向量
- leetcode:the skyline problem
- linux性能评测工具2-Google perftools
- 7.31 Django学习第三章
- spark源码分析--Master和worker建立连接
- 弗洛伊德算法
- HDU - 2066 一个人的旅行
- Reorder List --LeetCode
- windows10安装过程以及使用感受
- Android如何让软键盘出现和消失