Skip to content

Commit 4caaac2

Browse files
update 1395
1 parent 0033ba7 commit 4caaac2

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

Diff for: src/main/java/com/fishercoder/solutions/secondthousand/_1395.java

+14-22
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,54 @@
22

33
/**
44
* 1395. Count Number of Teams
5-
*
5+
* <p>
66
* There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
77
* You have to form a team of 3 soldiers amongst them under the following rules:
88
* Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
99
* A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
1010
* Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
11-
*
11+
* <p>
1212
* Example 1:
1313
* Input: rating = [2,5,3,4,1]
1414
* Output: 3
1515
* Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
16-
*
16+
* <p>
1717
* Example 2:
1818
* Input: rating = [2,1,3]
1919
* Output: 0
2020
* Explanation: We can't form any team given the conditions.
21-
*
21+
* <p>
2222
* Example 3:
2323
* Input: rating = [1,2,3,4]
2424
* Output: 4
25-
*
25+
* <p>
2626
* Constraints:
2727
* n == rating.length
2828
* 1 <= n <= 200
2929
* 1 <= rating[i] <= 10^5
30-
* */
30+
*/
3131
public class _1395 {
3232
public static class Solution1 {
3333
public int numTeams(int[] rating) {
34-
int count = 0;
35-
//check increasing
34+
int teams = 0;
3635
for (int i = 0; i < rating.length - 2; i++) {
3736
for (int j = i + 1; j < rating.length - 1; j++) {
38-
if (rating[j] > rating[i]) {
37+
if (rating[i] < rating[j]) {
3938
for (int k = j + 1; k < rating.length; k++) {
40-
if (rating[k] > rating[j]) {
41-
count++;
39+
if (rating[j] < rating[k]) {
40+
teams++;
4241
}
4342
}
44-
}
45-
}
46-
}
47-
48-
//check decreasing
49-
for (int i = 0; i < rating.length - 2; i++) {
50-
for (int j = i + 1; j < rating.length - 1; j++) {
51-
if (rating[j] < rating[i]) {
43+
} else if (rating[i] > rating[j]) {
5244
for (int k = j + 1; k < rating.length; k++) {
53-
if (rating[k] < rating[j]) {
54-
count++;
45+
if (rating[j] > rating[k]) {
46+
teams++;
5547
}
5648
}
5749
}
5850
}
5951
}
60-
return count;
52+
return teams;
6153
}
6254
}
6355
}

Diff for: src/test/java/com/fishercoder/secondthousand/_1395Test.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.fishercoder.secondthousand;
22

33
import com.fishercoder.solutions.secondthousand._1395;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _1395Test {
1010
private static _1395.Solution1 solution1;
1111

12-
@BeforeClass
13-
public static void setup() {
12+
@BeforeEach
13+
public void setup() {
1414
solution1 = new _1395.Solution1();
1515
}
1616

0 commit comments

Comments
 (0)