From 70c75e587a26b69320ca6e7326b1563703fe2448 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 29 Jul 2024 20:52:22 +0200 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB1395.=20Count=20Number=20of?= =?UTF-8?q?=20Teams=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- java/count-number-of-teams.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 java/count-number-of-teams.java diff --git a/java/count-number-of-teams.java b/java/count-number-of-teams.java new file mode 100644 index 0000000..4e23526 --- /dev/null +++ b/java/count-number-of-teams.java @@ -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; + } +}