Skip to content

Commit 0ae4d97

Browse files
add 1626
1 parent 379c73e commit 0ae4d97

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+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1626 {
6+
public static class Solution1 {
7+
public int bestTeamScore(int[] scores, int[] ages) {
8+
int len = scores.length;
9+
int[][] players = new int[len][2];
10+
for (int i = 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 = new int[len];
19+
dp[0] = players[0][1];
20+
for (int i = 1; i < len; i++) {
21+
int maxScoreUpToAgeI = players[i][1];//this is the max score possible on this age i alone
22+
for (int j = 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+
int bestScore = 0;
31+
for (int score : dp) {
32+
bestScore = Math.max(bestScore, score);
33+
}
34+
return bestScore;
35+
}
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1626;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1626Test {
10+
private static _1626.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1626.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(6, solution1.bestTeamScore(new int[]{1, 2, 3, 5}, new int[]{8, 9, 10, 1}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(34, solution1.bestTeamScore(new int[]{1, 3, 5, 10, 15}, new int[]{1, 2, 3, 4, 5}));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)