zoukankan      html  css  js  c++  java
  • LeetCode Weekly Contest 266

    第一题

    题解:模拟题

    class Solution {
    public:
        int countVowelSubstrings(string w) {
            int n = w.length();
            int i = 0, res = 0;
            string p = "aeiou";
            while(i < n){
                if(p.find(w[i]) != -1){
                        int j = i;
                        while(j < n && p.find(w[j]) != -1){
                            if(j < i+4) {
                                j++; 
                                continue;
                            }
                            string s = w.substr(i, j-i+1);
                            set<char> set;
                            for(auto c : s){
                                if(!set.count(c)){
                                    set.insert(c);
                                }
                            }
                            if(set.size() >=5){
                                res++;
                            }
                            j++;
                        }
                }
                i++;
            }
            return res;
        }
    };
    

    第二题

    题解:不能直接求子集判断超时,将问题转化为求每一个字母被使用的次数,第i个字母会在出现在 (n - i) * (i + 1) 个子串中

    class Solution {
    public:
        long long countVowels(string w) {
            string p = "aeiou";
            long long res = 0;
            for(int i=0; i<w.length(); i++){
                if(p.find(w[i]) != -1){
                    res += (long long)(i+1) *(w.length()-i);
                }
            }
            return res;
        }
    };
    

    第三题


    二分满足每一个商店商品数量 大于等于 k,通过商品数组中的商品数量计算店铺数量是否超过 n 来进行二分。

    class Solution {
    public:
        int minimizedMaximum(int n, vector<int>& q) {
            int l = 1;
            int r = 0;
            for(auto c : q) r = max(r, c);
            while(l < r){
                int mid = l+r >>1;
                if(check(q, mid, n)){
                    r = mid;
                }else{
                    l = mid+1;
                }
            }
            return r;
        }
    
        bool check(vector<int> q, int k, int n){
            int cnt = 0;
            for(auto c : q){
                if(c%k == 0){
                    cnt += c/k;
                }else cnt += (c/k)+1;
                if(cnt > n) return false;
            }
            return true;
        }
    };
    

    第四题


    题解:dfs, 注意 路径中节点值只能计算一次, 另外方法传递vector参数使用引用,不用最后一个用例超时了。

    #define PII pair<int, int>
    class Solution {
    public:
        unordered_map<int, vector<PII>> g;
        int res = 0, maxTimeV; 
        int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
            maxTimeV = maxTime;
            for(auto e : edges){
                // 无向边
                g[e[0]].push_back({e[1], e[2]});
                g[e[1]].push_back({e[0], e[2]});
            }
            vector<int> path; // 记录路径
            dfs(values, 0, 0, 0, path);
            return res;
        }
        void dfs(vector<int>& values,int u, int time, int value, vector<int> path){
            path.push_back(u);
            int prevVal = values[u];
            value += values[u];
            // 置为 0,节点只计算一次value
            values[u] = 0;
            if(u == 0){
                res = max(res, value);
            }
            for(auto c : g[u]){
                if(time + c.second > maxTimeV) continue;
                dfs(values, c.first, time+c.second, value, path);
            }
            // 恢复现场
            path.pop_back();
            values[u] = prevVal;
            value -= prevVal;
        }
    };
    
  • 相关阅读:
    矩阵分析 p13~p20
    矩阵分析 p1~p13
    复变函数2
    口语80篇(1-2)
    复变函数1
    背单词方法
    writing practice
    将博客搬至CSDN
    windows内核编程 白话设备栈
    sfliter__except_handler4
  • 原文地址:https://www.cnblogs.com/lzeffort/p/15521685.html
Copyright © 2011-2022 走看看