• 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);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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();
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    java 内部类
    非常详细的git-flow分支管理流程配置及使用
    UGUI DrawCall的优化 工作记录
    解决npm install报错: No module named gyp
    【深度学习】实现基于MNIST数据集的TensorFlow/Keras深度学习案例
    从零学算法(LCR 170)
    神经网络结构图怎么看的,神经网络结果图如何看
    Linux 的权限
    shell
    Nginx 防盗链
  • 原文地址:https://blog.csdn.net/ShowMeTheCod3/article/details/126859185