365天挑战LeetCode1000题——Day 025 周赛 银联专场


6112. 装满杯子需要的最短总时长(133)

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    int fillCups(vector<int>& amount) {
        int times = 0;
        priority_queue<int> pq;
        pq.push(amount[0]);
        pq.push(amount[1]);
        pq.push(amount[2]);
        while (pq.top() != 0) {
            int first = pq.top() - 1;
            pq.pop();
            int second = max(pq.top() - 1, 0);
            pq.pop();
            pq.push(first);
            pq.push(second);
            times++;
        }
        return times;
    }
};

6113. 无限集中的最小数字(134)

在这里插入图片描述

代码实现(自解)

class SmallestInfiniteSet {
public:
    set<int> s;
    priority_queue<int, vector<int>, greater<int>> pq;
    
    SmallestInfiniteSet() {
        for(int i = 1; i <= 1000; i++) {
            s.emplace(i);
            pq.push(i);
        }
    }
    
    int popSmallest() {
        int ans = pq.top();
        pq.pop();
        s.erase(ans);
        return ans;
    }
    
    void addBack(int num) {
        if (s.count(num)) return;
        s.emplace(num);
        pq.push(num);
    }
};

/**
 * Your SmallestInfiniteSet object will be instantiated and called as such:
 * SmallestInfiniteSet* obj = new SmallestInfiniteSet();
 * int param_1 = obj->popSmallest();
 * obj->addBack(num);
 */

6114. 移动片段得到字符串(135)

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    bool canChange(string start, string target) {
        // string s, t;
        vector<pair<char, int>> s, t;
        int n = start.size();
        for (int i = 0; i < n; i++) {
            if (start[i] != '_') s.push_back({start[i], i});
            if (target[i] != '_') t.push_back({target[i], i});
        }
        if (s.size() != t.size()) return false;
        n = s.size();
        for (int i = 0; i < n; i++) {
            if (s[i].first != t[i].first) return false;
            if (s[i].first == 'L' && s[i].second < t[i].second) return false;
            if (s[i].first == 'R' && s[i].second > t[i].second) return false;
        }
        return true;
    }
};

6115. 统计理想数组的数目(136)

在这里插入图片描述

代码实现(看题解)

class Solution {
    const int MOD = 1000000007;
    const int MAXP = 16;

public:
    int idealArrays(int n, int K) {
        // nlnn 求因数
        vector<vector<int>> fac(K + 1);
        for (int i = 1; i <= K; i++) for (int j = i << 1; j <= K; j += i) fac[j].push_back(i);

        // 计算子问题的答案
        vector<vector<long long>> f;        
        f.resize(K + 1, vector<long long>(20));
        for (int i = 1; i <= K; i++) {
            f[i][1] = 1;
            for (int j = 2; j <= MAXP; j++) for (int t : fac[i]) f[i][j] = (f[i][j] + f[t][j - 1]) % MOD;
        }

        // 求组合数
        vector<vector<long long>> C;
        C.resize(n + 1, vector<long long>(20));
        C[0][0] = C[1][0] = C[1][1] = 1;
        for (int i = 2; i <= n; i++) {
            C[i][0] = 1;
            for (int j = 1; j <= i && j <= MAXP; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
        }

        // 统计最终答案
        long long ans = 0;
        for (int i = 1; i <= K; i++) for (int j = 1; j <= MAXP; j++) ans = (ans + C[n - 1][j - 1] * f[i][j]) % MOD;
        return ans;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值