snippets 0.1.0
Loading...
Searching...
No Matches
prefix_suffix.hpp
Go to the documentation of this file.
1#ifndef PREFIX_SUFFIX_H
2#define PREFIX_SUFFIX_H
3
4#include <concepts>
5#include <vector>
6#include <algorithm>
7#include <numeric>
8
9namespace hsc_snippets
10{
22 template<std::integral T>
23 static std::vector<T> getPrefixMax(const std::vector<T> &v) {
24 if (v.empty()) {
25 return {};
26 }
27 std::vector<T> prefixMax(v.size());
28 prefixMax[0] = v[0];
29 for (size_t i = 1; i < v.size(); ++i) {
30 prefixMax[i] = std::max(prefixMax[i - 1], v[i]);
31 }
32 return prefixMax;
33 }
34
46 template<std::integral T>
47 static std::vector<T> getSuffixMax(const std::vector<T> &v) {
48 if (v.empty()) {
49 return {};
50 }
51 std::vector<T> suffixMax(v.size());
52 size_t n = v.size();
53 suffixMax[n - 1] = v[n - 1];
54 for (size_t i = n - 2; i < n; --i) {
55 suffixMax[i] = std::max(suffixMax[i + 1], v[i]);
56 }
57 return suffixMax;
58 }
59
71 template<std::integral T>
72 static std::vector<T> getPrefixMin(const std::vector<T> &v) {
73 if (v.empty()) {
74 return {};
75 }
76 std::vector<T> prefixMin(v.size());
77 prefixMin[0] = v[0];
78 for (size_t i = 1; i < v.size(); ++i) {
79 prefixMin[i] = std::min(prefixMin[i - 1], v[i]);
80 }
81 return prefixMin;
82 }
83
95 template<std::integral T>
96 static std::vector<T> getSuffixMin(const std::vector<T> &v) {
97 if (v.empty()) {
98 return {};
99 }
100 std::vector<T> suffixMin(v.size());
101 size_t n = v.size();
102 suffixMin[n - 1] = v[n - 1];
103 for (size_t i = n - 2; i < n; --i) {
104 suffixMin[i] = std::min(suffixMin[i + 1], v[i]);
105 }
106 return suffixMin;
107 }
108
120 template<std::integral T>
121 static std::vector<T> getPrefixSum(const std::vector<T> &v) {
122 if (v.empty()) {
123 return {};
124 }
125 std::vector<T> prefixSum(v.size());
126 std::partial_sum(v.begin(), v.end(), prefixSum.begin());
127 return prefixSum;
128 }
129
141 template<std::integral T>
142 static std::vector<T> getSuffixSum(const std::vector<T> &v) {
143 if (v.empty()) {
144 return {};
145 }
146 std::vector<T> suffixSum(v.size());
147 size_t n = v.size();
148 std::partial_sum(v.rbegin(), v.rend(), suffixSum.rbegin());
149 return suffixSum;
150 }
151}
152
153#endif // PREFIX_SUFFIX_H
Definition big_integer.hpp:14
static std::vector< T > getSuffixSum(const std::vector< T > &v)
Computes the suffix sums of a given vector.
Definition prefix_suffix.hpp:142
static std::vector< T > getSuffixMin(const std::vector< T > &v)
Computes the suffix minimums of a given vector.
Definition prefix_suffix.hpp:96
static std::vector< T > getSuffixMax(const std::vector< T > &v)
Computes the suffix maximums of a given vector.
Definition prefix_suffix.hpp:47
static std::vector< T > getPrefixMin(const std::vector< T > &v)
Computes the prefix minimums of a given vector.
Definition prefix_suffix.hpp:72
static std::vector< T > getPrefixMax(const std::vector< T > &v)
Computes the prefix maximums of a given vector.
Definition prefix_suffix.hpp:23
static std::vector< T > getPrefixSum(const std::vector< T > &v)
Computes the prefix sums of a given vector.
Definition prefix_suffix.hpp:121