Skip to content

Commit bc5a300

Browse files
add 1394
1 parent 4054d41 commit bc5a300

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-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+
|1395|[Count Number of Teams](https://leetcode.com/problems/count-number-of-teams/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1395.java) | |Medium|Array|
1112
|1394|[Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1394.java) | |Easy|Array|
1213
|1392|[Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1392.java) | |Hard|String, Rolling Hash|
1314
|1390|[Four Divisors](https://leetcode.com/problems/four-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1390.java) | |Medium|Math|
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1395. Count Number of Teams
5+
*
6+
* There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
7+
* You have to form a team of 3 soldiers amongst them under the following rules:
8+
* Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
9+
* A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
10+
* Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
11+
*
12+
* Example 1:
13+
* Input: rating = [2,5,3,4,1]
14+
* Output: 3
15+
* Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
16+
*
17+
* Example 2:
18+
* Input: rating = [2,1,3]
19+
* Output: 0
20+
* Explanation: We can't form any team given the conditions.
21+
*
22+
* Example 3:
23+
* Input: rating = [1,2,3,4]
24+
* Output: 4
25+
*
26+
* Constraints:
27+
* n == rating.length
28+
* 1 <= n <= 200
29+
* 1 <= rating[i] <= 10^5
30+
* */
31+
public class _1395 {
32+
public static class Solution1 {
33+
public int numTeams(int[] rating) {
34+
int count = 0;
35+
//check increasing
36+
for (int i = 0; i < rating.length - 2; i++) {
37+
for (int j = i + 1; j < rating.length - 1; j++) {
38+
if (rating[j] > rating[i]) {
39+
for (int k = j + 1; k < rating.length; k++) {
40+
if (rating[k] > rating[j]) {
41+
count++;
42+
}
43+
}
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]) {
52+
for (int k = j + 1; k < rating.length; k++) {
53+
if (rating[k] < rating[j]) {
54+
count++;
55+
}
56+
}
57+
}
58+
}
59+
}
60+
return count;
61+
}
62+
}
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1395;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1395Test {
10+
private static _1395.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1395.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.numTeams(new int[]{2, 5, 3, 4, 1}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(0, solution1.numTeams(new int[]{2, 1, 3}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(4, solution1.numTeams(new int[]{1, 2, 3, 4}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy