[LeetCode] 1395. Count Number of Teams

There are n soldiers standing in a line. Each soldier is assigned a unique rating value.

You have to form a team of 3 soldiers amongst them under the following rules:

  • Choose 3 soldiers with index (ijk) with rating (rating[i]rating[j]rating[k]).
  • A team is valid if:  (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).

Example 1:

Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). 

Example 2:

Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.

Example 3:

Input: rating = [1,2,3,4]
Output: 4

Constraints:

  • n == rating.length
  • 1 <= n <= 200
  • 1 <= rating[i] <= 10^5

统计作战单位数。

n 名士兵站成一排。每个士兵都有一个 独一无二 的评分 rating 。

每 3 个士兵可以组成一个作战单位,分组规则如下:

从队伍中选出下标分别为 i、j、k 的 3 名士兵,他们的评分分别为 rating[i]、rating[j]、rating[k]
作战单位需满足: rating[i] < rating[j] < rating[k] 或者 rating[i] > rating[j] > rating[k] ,其中  0 <= i < j < k < n
请你返回按上述条件可以组建的作战单位数量。每个士兵都可以是多个作战单位的一部分。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/count-number-of-teams
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题不涉及算法。我这里提供一个N平方的思路。这道题是在求一个三元组,满足下标i < j < k同时三个下标对应的rating是单调增或者是单调减。暴力解是O(n^3)的,需要对每一种i,j,k的组合进行比较看看是否符合条件。

优化的思路是O(n^2)的。这里我们对每个下标 j 记录他的左边有多少比他小的元素和他右边有多少比他大的元素。这样,包含 ratings[j] 的组合数 = j左边比他小的元素个数 * j右边比他大的元素个数。这是单调增的情况。

同时单调减的组合数 = j左边比他大的元素个数 * j右边比他小的元素个数。

时间O(n^2)

空间O(1)

Java实现

 1 class Solution {
 2     public int numTeams(int[] rating) {
 3         int res = 0;
 4         for (int i = 1; i < rating.length - 1; i++) {
 5             int leftBigger = 0;
 6             int leftSmaller = 0;
 7             for (int j = 0; j < i; j++) {
 8                 if (rating[j] > rating[i]) {
 9                     leftBigger++;
10                 }
11                 if (rating[j] < rating[i]) {
12                     leftSmaller++;
13                 }
14             }
15 
16             int rightBigger = 0;
17             int rightSmaller = 0;
18             for (int j = i + 1; j < rating.length; j++) {
19                 if (rating[j] > rating[i]) {
20                     rightBigger++;
21                 }
22                 if (rating[j] < rating[i]) {
23                     rightSmaller++;
24                 }
25             }
26             res += leftBigger * rightSmaller + leftSmaller * rightBigger;
27         }
28         return res;
29     }
30 }

 

LeetCode 题目总结

posted @ 2020-11-03 13:04  CNoodle  阅读(420)  评论(0)    收藏  举报