../ Competitive Programming
-rw-r--r--aguang838 B1 min read

Leet Code Daily 2024-07-29 - Count Number of Teams

Solution

  • Binary search.

Code

c++
class Solution {
public:
    int numTeams(vector<int>& rating) {

        set<int> LS, RS;
        for (const auto& x : rating) {
            RS.insert(x);
        }

        auto cnt_lt = [](set<int>& s, int x) -> int {
            return distance(s.begin(), s.lower_bound(x));
        };

        auto cnt_gt = [](set<int>& s, int x) -> int {
            return distance(s.upper_bound(x), s.end());
        };

        int ret = 0;
        for (const auto& x : rating) {
            RS.erase(x);
            ret += cnt_lt(LS, x) * cnt_gt(RS, x);
            ret += cnt_gt(LS, x) * cnt_lt(RS, x);
            LS.insert(x);
        }
        return ret;
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window