forked from codereport/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path138_Problem_P1.cpp
More file actions
31 lines (26 loc) · 811 Bytes
/
Copy path138_Problem_P1.cpp
File metadata and controls
31 lines (26 loc) · 811 Bytes
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
// code_report Solution
// Video Link: https://leetcode.com/contest/weekly-contest-138/problems/height-checker/
// Problem Link: https://youtu.be/i-KWz4ZHwFk
int heightChecker(vector<int>& h) {
auto t = h;
sort(t.begin(), t.end());
return inner_product(h.begin(), h.end(), t.begin(), 0,
plus<>(),
not_equal_to<>());
}
// C++20 Solution (Ranges-v3)
namespace rv = ranges::view;
namespace ra = ranges::action;
namespace hs {
template <typename P>
auto count_if(P p) {
return ranges::make_pipeable([&](auto&& rng) {
return ranges::count_if(rng, p);
});
}
}
auto heightChecker(vector<int> v) -> int {
auto t = v | ranges::copy | ra::sort;
return rv::zip_with(minus{}, v, t)
| hs::count_if([](auto e) { return e != 0; });
}