LeetCode双周赛11

1228. Missing Number In Arithmetic Progression

等差数列找出缺项。

class Solution {
public:
    int missingNumber(vector<int>& arr) {
        int vsize = arr.size();
        int n = (arr[vsize - 1] - arr[0] )/ vsize;
        int re = 0;
        for (int i = 1; i < vsize; i++) {
            if (arr[i - 1] + n != arr[i]) {
                re = arr[i - 1] + n;
                break;
            }
        }
         return re;
    }
   
};

1229. Meeting Scheduler

区间重合,需要两边都枚举

class Solution {
public:
    vector<int> slove(vector<vector<int>>& a, vector<vector<int>>& b, int d) {
         int j = 0;
        for (int i = 0; i < a.size() && j < b.size(); i++) {
            int a1 = a[i][0];
            int n1 = a1 + d;
            if (n1 > a[i][1]) continue;
            while (j < b.size() && n1 > b[j][1]) j++;
            if (j >= b.size()) return {};
            if (b[j][0] <= a1) return {a1, n1};
        }
        return {};
    }
    vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
        sort(slots1.begin(), slots1.end(), [&] (vector<int> a, vector<int> b) { return a[0] < b[0];});
        sort(slots2.begin(), slots2.end(), [&] (vector<int> a, vector<int> b) { return a[0] < b[0];});
        vector<int> re1 = slove(slots1, slots2, duration);
        vector<int> re2 = slove(slots2, slots1, duration);
        if (re1.size() == 0) return re2;
        if (re2.size() == 0) return re1;
        if (re1.size() != 0 && re2.size() != 0) {
            if (re1[0] < re2[0])
                return re1;
            else
                return re2;
        }
        return {};

    }
};

1230. Toss Strange Coins

投硬币投到相应值的概率

class Solution {
public:
    double dp[1001][1001] = {0};
    double probabilityOfHeads(vector<double>& prob, int target) {
        int vsize = prob.size();
        dp[0][1] = prob[0];
        dp[0][0] = 1 - prob[0];
        
        for (int i = 1; i < prob.size(); i++) {
            dp[i][0] = dp[i - 1][0] * (1- prob[i]);
            for (int j = 1; j <= i + 1; j++) {
                dp[i][j] = dp[i - 1][j - 1] * prob[i] + dp[i - 1][j] * (1 - prob[i]);
            }
        }
        return dp[prob.size() - 1][target];
    }
        
};

1231. Divide Chocolate

二分

class Solution {
public:
    bool canDiv(int m, vector<int>& sweetness, int K) {
        int cnt = 0, sum = 0;
        for (int n : sweetness) {
            sum += n;
            if (sum >= m) {
                sum = 0;
                cnt++;
            }
        }
        return cnt > K;
    }
    int maximizeSweetness(vector<int>& sweetness, int K) {
        int l = 0, h = 1e9 + 5;
        int re = 0;
        while (l <= h) {
            int m = l + (h - l) / 2;
            if (canDiv(m, sweetness, K)) {
                l = m + 1;
                re = max(re, m);
            } else {
                h = m - 1;
            }
        }
        return re;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值