Skip to content

Commit 3e510c2

Browse files
committed
PartitionTwo
1 parent 859fb80 commit 3e510c2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

util/partition.hh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
#include "util/fixed_vector.hh"
3+
#include <span>
4+
5+
template<unsigned MaxElements>
6+
struct PartitionTwo {
7+
8+
FixedVector<unsigned, MaxElements> a{};
9+
FixedVector<unsigned, MaxElements> b{};
10+
11+
PartitionTwo(std::span<unsigned> vals) {
12+
unsigned a_sum = 0;
13+
unsigned b_sum = 0;
14+
15+
// The part with the smaller sum gets the next element
16+
for (auto i = 0u; auto val : vals) {
17+
if (a_sum <= b_sum) {
18+
a_sum += val;
19+
a.push_back(i);
20+
} else {
21+
b_sum += val;
22+
b.push_back(i);
23+
}
24+
i++;
25+
}
26+
}
27+
};

0 commit comments

Comments
 (0)