<style> html, body, .ui-content { background: #222222; color: #00BFFF; } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } /* 設定連結 */ a, .open-files-container li.selected a { color: #89FFF8; } a:hover, .open-files-container li.selected a:hover { color: #89FFF890; } </style> ###### tags: `Leetcode` # 1626. Best Team With No Conflicts ###### Link : https://leetcode.com/problems/best-team-with-no-conflicts/description/ ## 題目 回傳最大的團隊分數,團隊中不能出現年紀較大的分數小於年紀較小的分數 ## 程式碼 ```cpp= class Solution { public: int bestTeamScore(vector<int>& scores, vector<int>& ages) { const int n = scores.size(); //用vector<pair<>>優先排序age再排序score vector<pair<int, int>> agescore; for(int i = 0;i < n;i++){ agescore.push_back({ages[i], scores[i]}); } sort(agescore.begin(), agescore.end()); vector<int> Score; for(auto &it : agescore){ Score.push_back(it.second); } //尋找最大分數 int maxScore = 0; for(int i = 0;i < n;i++){ for(int j = i - 1;j >= 0;j--){ //如果分數比年齡小的大,可以計算 if(agescore[i].second >= agescore[j].second) Score[i] = max(Score[i], agescore[i].second + Score[j]); } //更新maxScore maxScore = max(maxScore, Score[i]); } return maxScore; } }; ``` ## Date ### 2023/1/31