美文网首页
1395. Count Number of Teams

1395. Count Number of Teams

作者: 是嘤嘤嘤呀 | 来源:发表于2020-04-01 14:32 被阅读0次
image.png

解题思路:
1、直接暴力解:三层循环

class Solution {
public:
    int numTeams(vector<int>& rating) {
       int res = 0;
       int size = rating.size();
       for (int i = 0; i < size - 2; i++)
       for (int j = i + 1; j < size - 1; j++)
       for (int k = j + 1; k < size; k++) {
           if ((rating[i] < rating[j] && rating[j] < rating[k]) || (rating[i] > rating[j] && rating[j] > rating[k])) res ++;
       }
        return res;
    }
};

2、排列组合法

class Solution {
public:
   int numTeams(vector<int>& rating) {
      int res = 0;
      for (auto i = 1; i < rating.size(); ++i) {
        int lx1 = 0, lx2 = 0, gx1 = 0, gx2 = 0;
        for (auto j = 0; j < rating.size(); ++j) {
           if (i ==j ) continue;
           if (i < j ) {
               if (rating[i] < rating[j]) gx1++;
               else gx2 ++;
           } else {
               if (rating[i] < rating[j]) lx1++;
               else lx2 ++;
           }
         }
         res += lx1 * gx2 + gx1 * lx2;
     }
     return res;
   }
};

相关文章

网友评论

      本文标题:1395. Count Number of Teams

      本文链接:https://www.haomeiwen.com/subject/jzxauhtx.html