1395. Count Number of Teams Medium
1/**
2 * [1395] Count Number of Teams
3 *
4 * There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
5 * You have to form a team of 3 soldiers amongst them under the following rules:
6 *
7 * Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
8 * A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
9 *
10 * Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
11 *
12 * Example 1:
13 *
14 * Input: rating = [2,5,3,4,1]
15 * Output: 3
16 * Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
17 *
18 * Example 2:
19 *
20 * Input: rating = [2,1,3]
21 * Output: 0
22 * Explanation: We can't form any team given the conditions.
23 *
24 * Example 3:
25 *
26 * Input: rating = [1,2,3,4]
27 * Output: 4
28 *
29 *
30 * Constraints:
31 *
32 * n == rating.length
33 * 3 <= n <= 1000
34 * 1 <= rating[i] <= 10^5
35 * All the integers in rating are unique.
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/count-number-of-teams/
41// discuss: https://leetcode.com/problems/count-number-of-teams/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn num_teams(rating: Vec<i32>) -> i32 {
47 0
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_1395() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.