LeetCode周赛记录20200308

LeetCode周赛记录2020.3.8


从2020年1月31日正式到leetcode刷题,如今38天过去了,今日终于达成周赛全AC成就,开森o≥v≤o┴

5352 生成每种字符都是奇数个字符串(easy)

别被题目本身迷惑,其实就是个脑筋急转弯

class Solution {
public:
    string generateTheString(int n) {    
        string res;
        int h = n/2;
        if(n&1)
            res.append(n, 'a');
        else
        {
            if(h&1)
            {
                res.append(h, 'a');
                res.append(h, 'b');
            }
            else
            {
                res.append(h-1, 'a');
                res.append(h+1, 'b');
            }
        }
        return res;
    }
};

5353 灯泡开关3(mid)

提示:保存一个当前点亮灯泡的最大位置,和当前为蓝色灯泡的最大位置

class Solution {
public:
    int numTimesAllBlue(vector<int>& light) {
        int n = light.size();
        vector<int> st(n, 0);
        int maxL = -1, maxBL = -1, cnt = 0;
        for(auto l: light)
        {
            l = l-1;
            if(maxL<l)
                maxL = l;
            
            st[l] = 1;
            while(maxBL<n-1 && st[maxBL+1] == 1)
                ++maxBL;
            
            if(maxBL == maxL)
                ++cnt;
        }
        return cnt;
    }
};

5354 通知所有员工所需的时间(mid)

这不就是和网络延迟时间那题一样吗?

class Solution {
public:
    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
        vector<vector<int>> adj(n, vector<int>());
        vector<int> times(n, numeric_limits<int>::max());
        times[headID] = 0;
        for(int i=0;i<n;++i)
        {
            if(i == headID)
                continue;
            
            adj[manager[i]].push_back(i);
        }
        queue<int> q;
        q.push(headID);
        while(!q.empty())
        {
            auto t = q.front();
            q.pop();
            for(auto ad: adj[t])
            {
                if(times[ad]>times[t]+informTime[t])
                {
                    times[ad] = times[t]+informTime[t];
                    q.push(ad);
                }
            }
        }
        int res = numeric_limits<int>::min();
        for(auto t: times)
            res = max(res, t);
        
        return res;
    }
};

5355 T秒后青蛙的位置(hard)

无向树T层深度遍历,标记已到达过的点,函数退出时要取消标记(因为可能从另一条路径再次经过此处)

class Solution {
public:
    double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {
        vector<vector<int>> adj(n, vector<int>());
        for(const auto& e: edges)
        {
            adj[e[0]-1].push_back(e[1]-1);
            adj[e[1]-1].push_back(e[0]-1);
        }
        
        p=vector<double>(n, 0.0);
        vis = vector<int>(n, 0.0);
        frogP(t, 1.0, 0, adj);
        return p[target-1];     
    }
    
    void frogP(int t, double prob, int pos, vector<vector<int>>& adj)
    {
        if(t == 0)
        {
            p[pos] += prob;
            return;
        }
        
        double pp = prob/adj[pos].size();
        vis[pos] = 1;
        vector<int> candi;
        for(const auto& ad: adj[pos])
        {
            if(vis[ad] == 0)
                candi.push_back(ad);
        } 
        
        if(candi.empty())
            p[pos] += prob;
        else
        {
            double pp = prob/candi.size();
            for(auto c: candi)
                frogP(t-1, pp, c, adj);
        }
        
        vis[pos] = 0;
    }
    
    vector<double> p;
    vector<int> vis;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值