Best Team With No Conflicts
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.
However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age.
Given two lists, scores
and ages
, where each scores[i]
and ages[i]
represents the score and age of the ith
player, respectively, return the highest overall score of all possible basketball teams.
Example 1:
Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5] Output: 34 Explanation: You can choose all the players.
Example 2:
Input: scores = [4,5,6,5], ages = [2,1,2,1] Output: 16 Explanation: It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.
思路:首先按照年齡排序,然後按照score排序,最後就是求longest increasing subsequence,用DP解決,遞推公式就是j < i, 並且score[j] <= score[i] 就可以加上當前的score , dp[i] = Math.max(dp[i], dp[j] + nodes[i].score);
class Solution {
private class Node {
public int score;
public int age;
public Node (int score, int age) {
this.score = score;
this.age = age;
}
}
public int bestTeamScore(int[] scores, int[] ages) {
int n = scores.length;
Node[] nodes = new Node[n];
for(int i = 0; i < n; i++) {
nodes[i] = new Node(scores[i], ages[i]);
}
Arrays.sort(nodes, (a, b) -> (a.age != b.age ? a.age - b.age : a.score - b.score));
int[] dp = new int[n];
int res = 0;
for(int i = 0; i < n; i++) {
dp[i] = nodes[i].score;
for(int j = 0; j < i; j++) {
if(nodes[j].score <= nodes[i].score) {
dp[i] = Math.max(dp[i], dp[j] + nodes[i].score);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
}
相關文章
- css best practice for big team and projectCSSProject
- RAC Assurance Support Team: RAC and Oracle Clusterware Starter Kit and Best Practices (Generic)Oracle
- RAC Assurance Support Team: RAC and Oracle Clusterware Starter Kit and Best Practices (Generic) [IDOracle
- Team BuildingUI
- team building planUI
- Team Queue(佇列)佇列
- Best Practice in Writing
- Joe Harris is a real problem with this team
- Linux網路卡teamLinux
- Vue packages version conflicts 錯誤修復VuePackage
- Mobile Web Best Practices 1.0Web
- The best LeetCode NodesLeetCode
- 構建之法——Team & Scrum & MSFScrum
- Go team 訪談上線Go
- The Best Image Ocr SDK For BAT.BAT
- He also has best iphone casesiPhone
- 矩陣樹定理 BEST 定理矩陣
- Codeforces——231A Team
- 採用Hexo 搭建Team BlogHexo
- 招聘web development team leader!(上海)Webdev
- Team Foundation Server 安裝配置教程Server
- Team - 協同工具Confluence
- 【RMAN】RMAN-05001: auxiliary filename conflicts with the target databaseUXDatabase
- The Best Way to Export an SVG from SketchExportSVG
- Best Time to Buy and Sell Stock系列分析
- 80 of the Best Linux Security ApplicationsLinuxAPP
- Data Guard Switchover and Failover Best PracticesAI
- 矩陣樹定理與BEST定理矩陣
- [ARC060F] Best Representation
- [BT]Visual Studio Team System 2010 Team Suite Beta 1 (x86) - DVD (English)UI
- Hacking Team攻擊程式碼分析
- rhel7網路卡bond和team
- iOS signing for "***" requires a development team.iOSUIdev
- AKM專案逸事之Team BuildingUI
- how to check unsolved conflicts file list in git merge?Git
- RMAN-05517: temporary file conflicts with file used by target database(zt)Database
- [leetcode]Best Time to Buy and Sell StockLeetCode
- 25 Best Java Books In 2022Java