-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_score.cpp
More file actions
77 lines (66 loc) · 1.96 KB
/
maximum_score.cpp
File metadata and controls
77 lines (66 loc) · 1.96 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* =====================================================================================
*
* Filename: maximum_score.cpp
*
* Description: 2242. Maximum Score of a Node Sequence
*
* Version: 1.0
* Created: 11/18/2025 10:16:15
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <tuple>
#include <vector>
#include "gtest/gtest.h"
class Solution {
public:
int maximumScore(std::vector<int>& scores, std::vector<std::vector<int>>& edges) {
const int n = scores.size();
std::vector<std::vector<int>> neighbours(n);
for (auto e : edges) {
neighbours[e[0]].push_back(e[1]);
neighbours[e[1]].push_back(e[0]);
}
// Sort by descending order, O(nlog(n))
for (auto& nei : neighbours) {
std::sort(nei.begin(), nei.end(),
[&](const int i, const int j) -> bool { return scores[i] > scores[j]; });
}
int res = -1;
// Walk through every edge
for (auto& e : edges) {
int l = e[0];
int r = e[1];
int base = scores[l] + scores[r];
for (int i = 0; i < std::min(3, (int)neighbours[l].size()); i++) {
int x = neighbours[l][i];
if (x == r) {
continue;
}
for (int j = 0; j < std::min(3, (int)neighbours[r].size()); j++) {
int y = neighbours[r][j];
if (y == l || y == x) {
continue;
}
res = std::max(res, base + scores[x] + scores[y]);
}
}
}
return res;
}
};
TEST(Solution, maximumScore) {
std::vector<std::tuple<std::vector<int>, std::vector<std::vector<int>>, int>> cases = {
{{5, 2, 9, 8, 4}, {{0, 1}, {1, 2}, {2, 3}, {0, 2}, {1, 3}, {2, 4}}, 24},
};
for (auto& [scores, edges, res] : cases) {
EXPECT_EQ(Solution().maximumScore(scores, edges), res);
}
}