Skip to content

Commit f2f55bc

Browse files
authored
Create maximum-score-of-a-node-sequence.cpp
1 parent 7a680af commit f2f55bc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time: O(|V| + |E|)
2+
// Space: O(|V|)
3+
4+
// graph
5+
class Solution {
6+
public:
7+
int maximumScore(vector<int>& scores, vector<vector<int>>& edges) {
8+
using PII = pair<int, int>;
9+
using Heap = vector<PII>;
10+
const auto& find_top3 = [&scores](const auto& x, Heap *top3) {
11+
top3->emplace_back(scores[x], x); push_heap(begin(*top3), end(*top3), greater<PII>());
12+
if (size(*top3) > 3) {
13+
pop_heap(begin(*top3), end(*top3), greater<PII>()); top3->pop_back();
14+
}
15+
};
16+
vector<Heap> top3(size(scores));
17+
for (const auto& e : edges) {
18+
find_top3(e[1], &top3[e[0]]);
19+
find_top3(e[0], &top3[e[1]]);
20+
}
21+
int result = -1;
22+
for (const auto& e : edges) {
23+
const int a = e[0], b = e[1];
24+
for (const auto& [_, c] : top3[a]) {
25+
if (c == b) {
26+
continue;
27+
}
28+
for (const auto& [_, d] : top3[b]) {
29+
if (d == a || d == c) {
30+
continue;
31+
}
32+
result = max(result, scores[a] + scores[b] + scores[c] + scores[d]);
33+
}
34+
}
35+
}
36+
return result;
37+
}
38+
};

0 commit comments

Comments
 (0)