../ Competitive Programming
-rw-r--r--aguang3.0 KiB3 min read

Leet Code Weekly Contest 293

1. Find Resultant Array After Removing Anagrams

  • Bruce-force.
c++
class Solution {
public:
    vector<string> removeAnagrams(vector<string>& words) {
        vector<string> ans;
        ans.push_back(words.front());
        for (int i = 1; i < words.size(); i++) {
            string x = ans.back(), y = words[i];
            sort(x.begin(), x.end());
            sort(y.begin(), y.end());
            if (x != y) {
                ans.push_back(words[i]);
            }
        }
        return ans;
    }
};

2. Maximum Consecutive Floors Without Special Floors

  • Sort.
c++
class Solution {
public:
    int maxConsecutive(int bottom, int top, vector<int>& special) {
        vector<int> points = { bottom - 1, top + 1 };
        for (auto &x : special) {
            points.push_back(x);
        }
        sort(points.begin(), points.end());
        int ans = 0;
        for (int i = 1; i < points.size(); i++) {
            ans = max(ans, points[i] - points[i - 1] - 1);
        }
        return ans;
    }
};

3. Largest Combination With Bitwise AND Greater Than Zero

  • Enumerate the common bit.
c++
class Solution {
public:
    int largestCombination(vector<int>& candidates) {
        int ans = 0;
        for (int mask = 0; mask <= 30; mask++) {
            int cnt = 0;
            for (auto &x : candidates) {
                if (x & (1 << mask)) {
                    cnt++;
                }
            }
            ans = max(ans, cnt);
        }
        return ans;
    }
};

4. Count Integers in Intervals

  • Segment tree with lazy flag.
c++
struct Node {
    int left_, right_, cnt_;
    Node *lc_, *rc_;
    
    Node (int left, int right, int cnt = 0) {
        left_ = left;
        right_ = right;
        cnt_ = cnt;
        lc_ = NULL;
        rc_ = NULL;
    }
    
    int cnt() {
        return cnt_;
    }
    
    int size() {
        return right_ - left_ + 1;
    }
    
    int mid() {
        return (left_ + right_) >> 1;
    }
    
    Node *lc() {
        if (lc_ == NULL) {
            lc_ = new Node(left_, mid());
        }
        return lc_;
    }
    
    Node *rc() {
        if (rc_ == NULL) {
            rc_ = new Node(mid() + 1, right_);
        }
        return rc_;
    }
    
    void insert(int left, int right) {
        if (size() == cnt()) {
            return;
        }
        if (left <= left_ && right >= right_) {
            cnt_ = size();
            return;
        }
        if (left <= mid()) {
            lc()->insert(left, min(mid(), right));
        }
        if (right > mid()) {
            rc()->insert(max(mid() + 1, left), right);
        }
        cnt_ = 0;
        if (lc_ != NULL) {
            cnt_ += lc_->cnt();
        }
        if (rc_ != NULL) {
            cnt_ += rc_->cnt();
        }
    }
};

class CountIntervals {
private:
    Node *root;
    
public:
    CountIntervals() {
        root = new Node(1, 1E9);
    }
    
    void add(int left, int right) {
        root->insert(left, right);
    }
    
    int count() {
        return root->cnt();
    }
};

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