File tree 2 files changed +49
-7
lines changed
2 files changed +49
-7
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode-cn.com/problems/count-number-of-teams/
3
+ *
4
+ * 5369. 统计作战单位数
5
+ *
6
+ * Medium
7
+ */
1
8
const numTeams = rating => {
2
9
let ans = 0 ;
3
10
const max = rating . length ;
@@ -16,11 +23,4 @@ const numTeams = rating => {
16
23
}
17
24
18
25
return ans ;
19
- }
20
-
21
- function getCount ( count ) {
22
- if ( count < 3 ) {
23
- return 0 ;
24
- }
25
- return count * ( count - 1 ) * ( count - 2 ) / 6
26
26
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode-cn.com/problems/count-number-of-teams/
3
+ *
4
+ * 5369. 统计作战单位数
5
+ *
6
+ * Medium
7
+ *
8
+ * 64ms 100.00%
9
+ * 34.2mb 100.00%
10
+ */
11
+ const numTeams = rating => {
12
+ let ans = 0 ;
13
+ const max = rating . length ;
14
+ for ( let i = 1 ; i < max ; i ++ ) {
15
+ const middle = rating [ i ] ;
16
+ let c1 = i - 1 ;
17
+ let maxCount1 = 0 ;
18
+ let minCount1 = 0 ;
19
+ while ( c1 >= 0 ) {
20
+ if ( rating [ c1 ] < middle ) {
21
+ minCount1 ++ ;
22
+ } else if ( rating [ c1 ] > middle ) {
23
+ maxCount1 ++ ;
24
+ }
25
+ c1 -- ;
26
+ }
27
+
28
+ let c2 = i + 1 ;
29
+ let maxCount2 = 0 ;
30
+ let minCount2 = 0 ;
31
+ while ( c2 < max ) {
32
+ if ( rating [ c2 ] < middle ) {
33
+ minCount2 ++ ;
34
+ } else if ( rating [ c2 ] > middle ) {
35
+ maxCount2 ++ ;
36
+ }
37
+ c2 ++ ;
38
+ }
39
+ ans += ( minCount1 * maxCount2 ) + ( maxCount1 * minCount2 ) ;
40
+ }
41
+ return ans ;
42
+ }
You can’t perform that action at this time.
0 commit comments