Skip to content

Commit 344d609

Browse files
committed
Add solution #1395
1 parent eb3f5b0 commit 344d609

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,277 LeetCode solutions in JavaScript
1+
# 1,278 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1064,6 +1064,7 @@
10641064
1391|[Check if There is a Valid Path in a Grid](./solutions/1391-check-if-there-is-a-valid-path-in-a-grid.js)|Medium|
10651065
1392|[Longest Happy Prefix](./solutions/1392-longest-happy-prefix.js)|Hard|
10661066
1394|[Find Lucky Integer in an Array](./solutions/1394-find-lucky-integer-in-an-array.js)|Easy|
1067+
1395|[Count Number of Teams](./solutions/1395-count-number-of-teams.js)|Medium|
10671068
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10681069
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
10691070
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 1395. Count Number of Teams
3+
* https://leetcode.com/problems/count-number-of-teams/
4+
* Difficulty: Medium
5+
*
6+
* There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
7+
*
8+
* You have to form a team of 3 soldiers amongst them under the following rules:
9+
* - Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
10+
* - A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k])
11+
* where (0 <= i < j < k < n).
12+
*
13+
* Return the number of teams you can form given the conditions. (soldiers can be part of
14+
* multiple teams).
15+
*/
16+
17+
/**
18+
* @param {number[]} rating
19+
* @return {number}
20+
*/
21+
var numTeams = function(rating) {
22+
let result = 0;
23+
const n = rating.length;
24+
25+
for (let j = 1; j < n - 1; j++) {
26+
let leftLess = 0;
27+
let leftGreater = 0;
28+
let rightLess = 0;
29+
let rightGreater = 0;
30+
31+
for (let i = 0; i < j; i++) {
32+
if (rating[i] < rating[j]) leftLess++;
33+
else leftGreater++;
34+
}
35+
36+
for (let k = j + 1; k < n; k++) {
37+
if (rating[k] < rating[j]) rightLess++;
38+
else rightGreater++;
39+
}
40+
41+
result += leftLess * rightGreater + leftGreater * rightLess;
42+
}
43+
44+
return result;
45+
};

0 commit comments

Comments
 (0)