leetcode 1395. Count Number of Teams 统计作战单位数
Problem: 1395. Count Number of Teams 统计作战单位数
和索引放在一起,然后顺序排序的,三重循环只需要判断索引的大小是否一致即可
Code
class Solution { public: int numTeams(vector<int>& rating) { int n = rating.size(), ret = 0, a, b, c; vector<pair<int, int>> arr; for(int i = 0; i < n; i++) { arr.push_back({rating[i], i}); } sort(arr.begin(), arr.end()); for(int i = 0; i < n; i++) { a = arr[i].second; for(int j = i+1; j < n; j++) { b = arr[j].second; for(int k = j+1; k < n; k++) { if((a>b)==(b>arr[k].second)) ret++; } } } return ret; } };