365天挑战LeetCode1000题——Day 089 删除某些元素后的数组均值 设计位集 最大加号标志

1619. 删除某些元素后的数组均值

在这里插入图片描述

代码实现(STL)

class Solution {
public:
    double trimMean(vector<int>& arr) {
        sort(arr.begin(), arr.end());
        return accumulate(arr.begin() + (int)(0.05 * arr.size()), arr.begin() + (int)(0.95* arr.size()), 0) / (double)(arr.size() * 0.9);
    }
};

2166. 设计位集

在这里插入图片描述

代码实现

class Bitset {
private:
    string str;
    string flipped;
    int cnt;
public:
    Bitset(int size) {
        for (int i = 0; i < size; i++) {
            str += '0';
            flipped += '1';
        }
        cnt = size;
    }
    
    void fix(int idx) {
        if (str[idx] == '0') cnt--;
        str[idx] = '1';
        flipped[idx] = '0';
    }
    
    void unfix(int idx) {
        if (str[idx] == '1') cnt++;
        str[idx] = '0';
        flipped[idx] = '1';
    }
    
    void flip() {
        cnt = str.size() - cnt;
        string tmp = flipped;
        flipped = str;
        str = tmp;
    }
    
    bool all() {
        return !cnt;
    }
    
    bool one() {
        return cnt < str.size();       
    }
    
    int count() {
        return str.size() - cnt;
    }
    
    string toString() {
        return str;
    }
};

/**
 * Your Bitset object will be instantiated and called as such:
 * Bitset* obj = new Bitset(size);
 * obj->fix(idx);
 * obj->unfix(idx);
 * obj->flip();
 * bool param_4 = obj->all();
 * bool param_5 = obj->one();
 * int param_6 = obj->count();
 * string param_7 = obj->toString();
 */

764. 最大加号标志

在这里插入图片描述

代码实现(前缀和)

class Solution {
public:
    int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
        // if (n < 3) return
        vector<vector<int>> grid(vector(n, vector<int>(n, 1)));
        for (auto &arr : mines) {
            grid[arr[0]][arr[1]] = 0;
        }
        auto left = grid;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (left[i][j]) left[i][j] += left[i][j - 1];
            }
        }
        auto right = grid;
        for (int i = 0; i < n; i++) {
            for (int j = n - 2; j >= 0; j--) {
                if (right[i][j]) right[i][j] += right[i][j + 1];
            }
        }
        auto top = grid;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (top[i][j]) top[i][j] += top[i - 1][j];
            }
        }
        auto bottom = grid;
        for (int i = n - 2; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                if (bottom[i][j]) bottom[i][j] += bottom[i + 1][j];
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ans = max(ans, min(min(left[i][j], right[i][j]),
                min(top[i][j], bottom[i][j])));
                // cout << i << " " << j << " " << ans << endl;
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值