forked from codereport/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101Hack55_P3.cpp
More file actions
89 lines (70 loc) · 2.51 KB
/
Copy path101Hack55_P3.cpp
File metadata and controls
89 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// code_report Solution
// https://youtu.be/czzx3pVAdw4
// Original solution
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <set>
#include <functional>
#include <numeric>
#include <type_traits>
using namespace std;
long fewestTowers (std::vector<int> xs, std::vector<int> ys)
{
map<int, set<int>> xy;
int n = xs.size ();
for (int i = 0; i < n; ++i) {
int x = xs[i];
int y = ys[i];
xy[x].insert (y);
}
int c = xy.size (); // cols
vector<int> t, b; // top, bottom
for (const auto& col : xy) {
b.push_back (*col.second.begin ());
t.push_back (*col.second.rbegin ());
}
auto j = distance (b.begin (), min_element (b.begin (), b.end ()));
auto k = distance (t.begin (), max_element (t.begin (), t.end ()));
for (int i = 1; i <= j; ++i) b[i] = min (b[i], b[i - 1]);
for (int i = 1; i <= k; ++i) t[i] = max (t[i], t[i - 1]);
for (int i = c-2; i >= j; --i) b[i] = min (b[i], b[i + 1]);
for (int i = c-2; i >= k; --i) t[i] = max (t[i], t[i + 1]);
long ans = -n;
for (auto i = 0; i < c; ++i) ans += t[i] - b[i] + 1; // could be transform
return ans;
}
// Modern solution
template <typename I, typename P, typename F>
F group_equal (I f, I l, P p, F action) {
while (f != l) {
I cur_end = std::upper_bound (f, l, *f, p);
action (f, cur_end);
f = cur_end;
}
return action;
}
template <typename I, typename P>
void set_perimeter (I f, I l, P p) {
using rev = std::reverse_iterator<I>;
auto limit = min_element (f, l, p);
auto min_p = [p](const auto& x, const auto& y) { return p (x, y) ? x : y; };
std::partial_sum (f, limit, f, min_p);
std::partial_sum (rev (l), rev (limit), rev (l), min_p);
}
long fewestTowers (std::vector<int> xs, std::vector<int> ys)
{
std::vector<std::pair<int, int>> dots (xs.size ());
std::transform (xs.begin (), xs.end (), ys.begin (), dots.begin (), [](int x, int y) { return std::make_pair (x, y); });
std::sort (dots.begin (), dots.end ());
std::vector<int> t, b; // top, bottom
group_equal (dots.begin (), dots.end (), [](const auto& dot1, const auto& dot2) { return dot1.first < dot2.first; },
[&](auto f, auto l) mutable { b.push_back (f->second); t.push_back ((--l)->second); });
set_perimeter (b.begin (), b.end (), std::less<> ());
set_perimeter (t.begin (), t.end (), std::greater<> ());
long ans = - (int) xs.size ();
for (auto i = 0; i < t.size (); ++i) ans += t[i] - b[i] + 1;
return ans;
}