|
45 | 45 | <li><code>1 <= ages[i] <= 1000</code></li>
|
46 | 46 | </ul>
|
47 | 47 |
|
48 |
| - |
49 | 48 | ## 解法
|
50 | 49 |
|
51 | 50 | <!-- 这里可写通用的实现逻辑 -->
|
52 | 51 |
|
| 52 | +动态规划 - 最长上升子序列模型。 |
| 53 | + |
| 54 | +将所有球员先按照年龄从小到大排序(年龄相同,则按照分数从小到大排),然后在分数数组中求解最长上升子序列和的最大值即可。 |
| 55 | + |
| 56 | +类似题型:洛谷 “[P2782 友好城市](https://www.luogu.com.cn/problem/P2782)”。 |
| 57 | + |
53 | 58 | <!-- tabs:start -->
|
54 | 59 |
|
55 | 60 | ### **Python3**
|
56 | 61 |
|
57 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 63 |
|
59 | 64 | ```python
|
60 |
| - |
| 65 | +class Solution: |
| 66 | + def bestTeamScore(self, scores: List[int], ages: List[int]) -> int: |
| 67 | + nums = list(zip(scores, ages)) |
| 68 | + nums.sort(key=lambda x: (x[1], x[0])) |
| 69 | + n = len(ages) |
| 70 | + dp = [nums[i][0] for i in range(n)] |
| 71 | + res = 0 |
| 72 | + for i in range(n): |
| 73 | + for j in range(i): |
| 74 | + if nums[j][0] <= nums[i][0]: |
| 75 | + dp[i] = max(dp[i], dp[j] + nums[i][0]) |
| 76 | + res = max(res, dp[i]) |
| 77 | + return res |
61 | 78 | ```
|
62 | 79 |
|
63 | 80 | ### **Java**
|
64 | 81 |
|
65 | 82 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 83 |
|
67 | 84 | ```java
|
| 85 | +class Solution { |
| 86 | + public int bestTeamScore(int[] scores, int[] ages) { |
| 87 | + int n = ages.length; |
| 88 | + int[][] nums = new int[n][2]; |
| 89 | + for (int i = 0; i < n; ++i) { |
| 90 | + nums[i] = new int[]{scores[i], ages[i]}; |
| 91 | + } |
| 92 | + Arrays.sort(nums, (a, b) -> { |
| 93 | + return a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]; |
| 94 | + }); |
| 95 | + int[] dp = new int[n]; |
| 96 | + int res = 0; |
| 97 | + for (int i = 0; i < n; ++i) { |
| 98 | + dp[i] = nums[i][0]; |
| 99 | + for (int j = 0; j < i; ++j) { |
| 100 | + if (nums[j][0] <= nums[i][0]) { |
| 101 | + dp[i] = Math.max(dp[i], dp[j] + nums[i][0]); |
| 102 | + } |
| 103 | + } |
| 104 | + res = Math.max(res, dp[i]); |
| 105 | + } |
| 106 | + return res; |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### **C++** |
| 112 | + |
| 113 | +```cpp |
| 114 | +class Solution { |
| 115 | +public: |
| 116 | + int bestTeamScore(vector<int> &scores, vector<int> &ages) { |
| 117 | + vector<pair<int, int>> nums; |
| 118 | + int n = ages.size(); |
| 119 | + for (int i = 0; i < n; ++i) nums.push_back({scores[i], ages[i]}); |
| 120 | + sort(nums.begin(), nums.end(), [](auto &a, auto &b) { |
| 121 | + return a.second == b.second ? a.first < b.first : a.second < b.second; |
| 122 | + }); |
| 123 | + vector<int> dp(n); |
| 124 | + int res = 0; |
| 125 | + for (int i = 0; i < n; ++i) |
| 126 | + { |
| 127 | + dp[i] = nums[i].first; |
| 128 | + for (int j = 0; j < i; ++j) |
| 129 | + { |
| 130 | + if (nums[j].first <= nums[i].first) |
| 131 | + dp[i] = max(dp[i], dp[j] + nums[i].first); |
| 132 | + } |
| 133 | + res = max(res, dp[i]); |
| 134 | + } |
| 135 | + return res; |
| 136 | + } |
| 137 | +}; |
| 138 | +``` |
68 | 139 |
|
| 140 | +### **Go** |
| 141 | +
|
| 142 | +```go |
| 143 | +func bestTeamScore(scores []int, ages []int) int { |
| 144 | + n := len(ages) |
| 145 | + var nums [][]int |
| 146 | + for i := 0; i < n; i++ { |
| 147 | + nums = append(nums, []int{scores[i], ages[i]}) |
| 148 | + } |
| 149 | + sort.Slice(nums, func(i, j int) bool { |
| 150 | + if nums[i][1] == nums[j][1] { |
| 151 | + return nums[i][0] < nums[j][0] |
| 152 | + } |
| 153 | + return nums[i][1] < nums[j][1] |
| 154 | + }) |
| 155 | + dp := make([]int, n) |
| 156 | + res := 0 |
| 157 | + for i := 0; i < n; i++ { |
| 158 | + dp[i] = nums[i][0] |
| 159 | + for j := 0; j < i; j++ { |
| 160 | + if nums[j][0] <= nums[i][0] { |
| 161 | + dp[i] = max(dp[i], dp[j]+nums[i][0]) |
| 162 | + } |
| 163 | + } |
| 164 | + res = max(res, dp[i]) |
| 165 | + } |
| 166 | + return res |
| 167 | +} |
| 168 | +
|
| 169 | +func max(a, b int) int { |
| 170 | + if a > b { |
| 171 | + return a |
| 172 | + } |
| 173 | + return b |
| 174 | +} |
69 | 175 | ```
|
70 | 176 |
|
71 | 177 | ### **...**
|
|
0 commit comments