codekofi
← All questions

Merge Triplets to Form Target Triplet

HardGreedy

The problem

Merging two triplets replaces each position with the larger of the two values there. You may merge any triplets you like, any number of times, in any order.

Return whether the target triplet can be produced.

mergeTriplets({{2,5,3},{1,8,4},{1,7,5}}, {2,7,5})

One of these three is correct

Two lines apart, at the closest.

The wrong ones are this same code with between two and five lines changed. Some of those changes do not compile. There is no Run button: running all three would turn this into a vote rather than a reading.

1bool mergeTriplets(const vector<vector<int>>& triplets,
2 const vector<int>& target) {
3
4 bool a = false;
5 bool b = false;
6 bool c = false;
7
8 for (const auto& t : triplets) {
9
10 if (t[0] > target[0] || t[1] > target[1] ||
11 t[2] > target[2]) {
12 break;
13 }
14
15 if (t[0] == target[0]) {
16 a = true;
17 }
18
19 if (!(t[1] == target[1])) {
20 b = true;
21 }
22
23 if (t[2] == target[2]) {
24 c = true;
25 }
26 }
27
28 return a && b && c;
29}
1bool mergeTriplets(const vector<vector<int>>& triplets,
2 const vector<int>& target) {
3
4 bool a = false;
5 bool b = false;
6 bool c = false;
7
8 for (const auto& t : triplets) {
9
10 if (t[0] > target[0] || t[1] > target[1] ||
11 t[2] > target[2]) {
12 continue;
13 }
14
15 if (t[0] == target[0]) {
16 a = true;
17 }
18
19 if (t[1] == target[1]) {
20 b = true;
21 }
22
23 if (t[2] == target[2]) {
24 c = true;
25 }
26 }
27
28 return a && b && c;
29}
1bool mergeTriplets(const vector<vector<int>>& triplets,
2 const vector<int>& target) {
3
4 bool a = false;
5 bool b = false;
6 bool c = false;
7
8 for (const auto& t : triplets) {
9
10 if (t[0] > target[0] || t[1] > target[1] ||
11 t[2] > target[2]) {
12 break;
13 }
14
15 if (t[0] == target[0]) {
16 a = true;
17 }
18
19 if (!(t[1] == target[1])) {
20 b = true;
21 }
22
23 if (!(t[2] == target[2])) {
24 c = true;
25 }
26 }
27
28 return a && b && c;
29}