第 179 场周赛 Posted on 2020-03-08 | In leetcode , Weekly-Contest | | Words count in article: 452 | Reading time ≈ 2 1374. 生成每种字符都是奇数个的字符串12345678910class Solution {public: string generateTheString(int n) { if(n & 1){ return string(n,'a'); }else{ return string(n-1,'a') + "b"; } }}; 1375. 灯泡开关 III1234567891011class Solution {public: int numTimesAllBlue(vector<int>& light) { int m=0, cnt=0; for(int i=0; i<light.size(); i++) { m = max(light[i], m); if(m == i+1) cnt++; } return cnt; }}; 1376. 通知所有员工所需的时间12345678910111213141516171819202122class Solution {public: int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) { vector<vector<int>> my_son(manager.size()); for(int i=0;i<manager.size();i++){ if(manager[i]!=-1) my_son[manager[i]].push_back(i); } queue<pair<int,int>> Q; Q.push({headID,0}); int max=0; pair<int,int> cur; while(!Q.empty()){ cur=Q.front(); Q.pop(); if(cur.second>max) max=cur.second; for(auto it:my_son[cur.first]){ Q.push({it,cur.second+informTime[cur.first]}); } } return max; }}; 1377. T 秒后青蛙的位置12345678910111213141516171819202122232425262728293031323334353637383940414243444546class Solution {public: struct node { int id; double p; int t; node(int a, double b, int c) { id = a; p = b; t = c; } }; double frogPosition(int n, vector<vector<int>>& edges, int t, int target) { vector<int> vis(n+1); vector<vector<int>> gh(n+1); for(auto& edge: edges) { gh[edge[0]].push_back(edge[1]); gh[edge[1]].push_back(edge[0]); } queue<node> q; q.push(node(1, 1.0, 0)); vis[1] = 1; while(!q.empty()) { node u = q.front(); q.pop(); if(u.id == target && u.t == t) return u.p; int sz = 0; for(int i=0; i<gh[u.id].size(); i++) { if(vis[gh[u.id][i]] == 0) { sz++; } } if(u.t < t) { int flag = 0; for(int i=0; i<gh[u.id].size(); i++) { if(vis[gh[u.id][i]] == 0) { flag = 1; vis[gh[u.id][i]] = 1; q.push(node(gh[u.id][i], u.p/sz, u.t+1)); } } if(flag == 0) { q.push(node(u.id, u.p, u.t+1)); } } } return 0.0; }}; Donate? comment? Donate WeChat Pay Alipay