Skip to content

Commit 3cce5f9

Browse files
committed
Add Q218, Q219, Q220
1 parent 76a15d4 commit 3cce5f9

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//https://discuss.leetcode.com/topic/25794/17-line-o-n-log-n-time-o-n-space-c-accepted-easy-solution-w-explanations
2+
class Solution {
3+
public:
4+
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
5+
multimap<int, int> coords;
6+
for (auto&& building : buildings) {
7+
coords.emplace(building[0], building[2]);
8+
coords.emplace(building[1], -building[2]);
9+
}
10+
11+
multiset<int> heights = {0};
12+
map<int, int> corners;
13+
for (auto&& p : coords) {
14+
if (p.second > 0) {
15+
heights.insert(p.second);
16+
} else {
17+
heights.erase(heights.find(-p.second));
18+
}
19+
corners[p.first] = *heights.rbegin();
20+
}
21+
auto eq2nd = [](pair<int, int> l, pair<int, int> r){ return l.second == r.second; };
22+
vector<pair<int, int>> results;
23+
unique_copy(corners.begin(), corners.end(), back_insert_iterator<vector<pair<int, int>>>(results), eq2nd);
24+
25+
return results;
26+
}
27+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
4+
map<int, int> m;
5+
for (int i = 0; i < nums.size(); ++i) {
6+
if (m.find(nums[i]) != m.end()) {
7+
if (i - m[nums[i]] <= k) return true;
8+
m[nums[i]] = i;
9+
} else {
10+
m[nums[i]] = i;
11+
}
12+
}
13+
return false;
14+
}
15+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
4+
if (t < 0) return false;
5+
unordered_map<long, long> m;
6+
#define BIN(i, t) (i < 0 ? (i+1)/(t+1)-1 : i/(t+1))
7+
for (int i = 0; i < nums.size(); ++i) {
8+
int b = BIN(nums[i], t);
9+
if (m.find(b) != m.end()) {
10+
return true;
11+
}
12+
if (m.find(b-1) != m.end() && abs(nums[i] - m[b-1]) <= t) {
13+
return true;
14+
}
15+
if (m.find(b+1) != m.end() && abs(nums[i] - m[b+1]) <= t) {
16+
return true;
17+
}
18+
m[b] = nums[i];
19+
20+
if (i >= k) {
21+
m.erase(BIN(nums[i-k], t));
22+
}
23+
}
24+
return false;
25+
}
26+
};

0 commit comments

Comments
 (0)