mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
java: add «1395. Count Number of Teams»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
0d8ce53175
commit
70c75e587a
1 changed files with 32 additions and 0 deletions
32
java/count-number-of-teams.java
Normal file
32
java/count-number-of-teams.java
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
class Solution {
|
||||||
|
public int numTeams(int[] rating) {
|
||||||
|
int[][][] dp = new int[2][3][rating.length];
|
||||||
|
|
||||||
|
// base
|
||||||
|
for (int i = 0; i < rating.length; ++i) {
|
||||||
|
dp[0][0][i] = 1;
|
||||||
|
dp[1][0][i] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int count = 2; count <= 3; ++count) {
|
||||||
|
for (int i = 0; i < rating.length; ++i) {
|
||||||
|
for (int j = i + 1; j < rating.length; ++j) {
|
||||||
|
if (rating[i] < rating[j]) {
|
||||||
|
dp[0][count - 1][j] += dp[0][count - 2][i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rating[i] > rating[j]) {
|
||||||
|
dp[1][count - 1][j] += dp[1][count - 2][i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int teams = 0;
|
||||||
|
for (int i = 0; i < rating.length; ++i) {
|
||||||
|
teams += dp[0][2][i] + dp[1][2][i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return teams;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue