Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ae4d97

Browse files
add 1626
1 parent379c73e commit0ae4d97

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1626|[Best Team With No Conflicts](https://leetcode.com/problems/best-team-with-no-conflicts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1626.java)||DP|Medium|
1112
|1625|[Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1625.java)||BFS, DFS|Medium|
1213
|1624|[Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1624.java)||String|Easy|
1314
|1620|[Coordinate With Maximum Network Quality](https://leetcode.com/problems/coordinate-with-maximum-network-quality/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1620.java)||Greedy|Medium|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
publicclass_1626 {
6+
publicstaticclassSolution1 {
7+
publicintbestTeamScore(int[]scores,int[]ages) {
8+
intlen =scores.length;
9+
int[][]players =newint[len][2];
10+
for (inti =0;i <len;i++) {
11+
players[i][0] =ages[i];
12+
players[i][1] =scores[i];
13+
}
14+
//sort by age first, if tie, then sort by scores
15+
Arrays.sort(players, (a,b) ->a[0] !=b[0] ?a[0] -b[0] :a[1] -b[1]);
16+
17+
//dp array is the max possible score up to this age, i.e. dp[2] means the max score up to from age 0 up to age 2
18+
int[]dp =newint[len];
19+
dp[0] =players[0][1];
20+
for (inti =1;i <len;i++) {
21+
intmaxScoreUpToAgeI =players[i][1];//this is the max score possible on this age i alone
22+
for (intj =0;j <i;j++) {
23+
//then we try to find all possible scores from the min age up to this age i
24+
if (players[i][1] >=players[j][1]) {
25+
maxScoreUpToAgeI =Math.max(maxScoreUpToAgeI,dp[j] +players[i][1]);
26+
}
27+
}
28+
dp[i] =maxScoreUpToAgeI;
29+
}
30+
intbestScore =0;
31+
for (intscore :dp) {
32+
bestScore =Math.max(bestScore,score);
33+
}
34+
returnbestScore;
35+
}
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1626;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.TestCase.assertEquals;
8+
9+
publicclass_1626Test {
10+
privatestatic_1626.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1626.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(6,solution1.bestTeamScore(newint[]{1,2,3,5},newint[]{8,9,10,1}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(34,solution1.bestTeamScore(newint[]{1,3,5,10,15},newint[]{1,2,3,4,5}));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp