leetcode267

https://leetcode-cn.com/contest/weekly-contest-267/
2073. 买票需要的时间


// 暴力
class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int n = tickets.size();
        int ans = 0;
        while (tickets[k] != 0) {
            for (int i = 0; i < n; i++) {
                if (tickets[i] > 0) {
                    tickets[i]--;
                    ans++;
                }
                if (tickets[k] == 0) {
                    return ans;
                }
            }
        }
        return 0;
    }
};
​
​
// 一次遍历,k前面的人最多买t[k]张票,k后面的人最多买t[k] - 1张票
class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int n = tickets.size();
        int res = 0;
        for (int i = 0; i < n; i++) {
            if (i <= k) {
                res += min(tickets[i], tickets[k]);
            } else {
                res += min(tickets[i], tickets[k] - 1);
            }
        }
        return res;
    }
};
  1. 反转偶数长度组的节点
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
​
// 原地翻转
class Solution {
public:
    ListNode* reverseEvenLengthGroups(ListNode* head) {
        ListNode* dummy = new ListNode(0, head);
        ListNode* pre = dummy;
        ListNode* cur = head;
        int count = 0;
        while (cur != nullptr) {
            ++count;
            ListNode* tmp = cur;
            int len = 0;
            while (len < count && tmp != nullptr) {
                ++len;
                tmp = tmp->next;
            }
            if (len % 2 == 0) {
                for (int i = 0; i < len - 1; i++) {
                    ListNode* removeNode = cur->next;
                    cur->next = cur->next->next;
                    removeNode->next = pre->next;
                    pre->next = removeNode;
                }
                pre = cur;
                cur = cur->next;
            } else {
                for (int i = 0; i < len; i++) {
                    cur = cur->next;
                    pre = pre->next;
                }
            }
        }
        return dummy->next;
    }
};
  1. 解码斜向换位密码
// 无脑模拟
class Solution {
public:
    string decodeCiphertext(string encodedText, int rows) {
        int col = encodedText.size() / rows;
        int row = rows;
        int i = 0, j = 0;
        string ans;
​
        while (i < row && j < col) {
            // 向右下方遍历
            ans += encodedText[i * col + j];
            i++;
            j++;
            // 到头后回去
            if (i == row) {
                i = 0;
                j -= (row - 1);
            }
        }
        while (ans.back() == ' ') {
            ans.pop_back();
        }
        return ans;
    }
};
  1. 处理含限制条件的好友请求
// 并查集模板
class UnionSet {
public:
    UnionSet(int n) {
        parent.reserve(n);
        _size.reserve(n);
​
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            _size[i] = 1;
        }
    }
​
    int Find(int x) {
        if (parent[x] == x) {
            return parent[x];
        } else {
            return Find(parent[x]);
        }
    }
​
    void Merge(int x, int y) {
        x = Find(x);
        y = Find(y);
        if (x != y) {
            if (_size[x] < _size[y]) {
                swap(x, y);
            }
            parent[y] = x;
            _size[x] += _size[y];
        }
    }
​
    bool Connect(int x, int y) {
        x = Find(x);
        y = Find(y);
​
        return x == y;
    }
​
private:
    vector<int> parent;
    vector<int> _size;
};
​
class Solution {
public:
    vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {
        vector<bool> ans;
        UnionSet us(n);
​
        // 对于每一个请求,1.如果已经在一个圈子,请求成功
        //               2.如果不在一个圈子,遍历restriction,如果两个分别和限制一个圈子,
        //                 请求失败,否则成功
        for (auto& request : requests) {
            int x = us.Find(request[0]), y = us.Find(request[1]);
            // 1.
            if (us.Connect(x, y)) {
                ans.push_back(true);
                continue;
            }
            bool isOk = true;
            // 2.
            for (auto& restr : restrictions) {
                int r1 = us.Find(restr[0]), r2 = us.Find(restr[1]);
                if ((r1 == x && r2 == y) || (r1 == y && r2 == x)) {
                    isOk = false;
                    ans.push_back(false);
                    break;
                }
            }
            if (isOk) {
                us.Merge(x, y);
                ans.push_back(true);
            }
        }
        return ans;
    }
};
posted @ 2021-11-23 11:52  Wang~ze君  阅读(85)  评论(0)    收藏  举报