5352. Each generates a string of characters is an odd number
1 class Solution { 2 public: 3 string generateTheString(int n) { 4 string ans=""; 5 if(n%2==1){ 6 for(int i=0;i<n;i++){ 7 ans+='a'; 8 } 9 }else{ 10 for(int i=0;i<n-1 ; i ++ ) { 11 years + = ' a ' ; 12 } 13 years + = ' b ' ; 14 } 15 return year; 16 } 17 };
Lamp switch 5353. III
Solution one: to find the law: When the position of the largest lighting with the same amount of time lights, then light blue:
. 1 class Solution { 2 public : . 3 /// when an equal number of positions and the maximum turn-on time of lighting, then it turned blue light . 4 int numTimesAllBlue (Vector < int > & Light) { . 5 int n-= Light .size (); . 6 int MAX_NUM = 0 ; // record the maximum position of the bright light . 7 int CNT = 0 ; // number of recording lights . 8 int ANS = 0 ; . 9 for ( int I = 0 ; I <n- ; I ++ ) { 10 MAX_NUM =max(max_num,light[i]); 11 cnt++; 12 if(cnt==max_num){ 13 ans++; 14 } 15 } 16 return ans; 17 } 18 };
Solution two: disjoint-set:
. 1 const int MAXN = . 5 * 1E4 + 100 ; 2 class Solution { . 3 public : . 4 int Father [MAXN]; . 5 int Find ( int X) { . 6 // return Father [X] = (Father [X] == X? X: Find (Father [X])); . 7 the while ! (Father [X] = X) { . 8 X = Father [X]; . 9 } 10 return X; . 11 } 12 is // not need to write this function is combined, because there will be following the merger, and the merger is small to large, that is, when a large father. . 13 int numTimesAllBlue(vector<int>& light) { 14 int n=light.size(); 15 int ans=0; 16 int op[n+1]; 17 for(int i=1;i<=n;i++){ 18 father[i]=i; 19 op[i]=0; 20 } 21 int cnt=0; 22 for(int i=0;i<n;i++){ 23 int NUM = Light [I]; 24 OP [NUM] = . 1 ; 25 26 is IF (NUM> . 1 && OP [num- . 1 ]) [num-Father . 1 ] = NUM; // represented by one lamp before the lamp current so is the amount, which is incorporated into the current lamp ranks. 27 IF (NUM + . 1 <= n-&& OP [NUM + . 1 ]) Father [NUM] = NUM + . 1 ; // large copy to his father 28 29 CNT ++ ; 30 IF (OP [ . 1 ] && Find ( . 1 ) == CNT) ANS ++ ; 31 is } 32 returnyears; 33 } 34 };
5354. notification time required for all employees
1 const int maxn=1e5+50; 2 //vector<int>dist(maxn); 3 4 class Solution { 5 public: 6 7 int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) { 8 vector<int>dist(maxn,0); 9 vector<int>edge[maxn]; 10 int ans=0; 11 IF (n-<= . 1 ) return 0 ; 12 is // traverse the level bit, and bob: 13 is 14 // int = n-manager.size (); 15 for ( int I = 0 ; I <n-; I ++ ) { 16 dist [ I] = 0 ; . 17 IF (Manager [I] = -! . 1 ) Edge [Manager [I]] push_back (I);. 18 is } . 19 Queue < int > que; 20 is que.push (headID); 21 is the while ( ! que.empty ()) { // BFS template 22 int x=que.front(); 23 que.pop(); 24 ans=max(ans,dist[x]); 25 for(int i=0;i<edge[x].size();i++){ 26 int v=edge[x][i]; 27 dist[v]=dist[x]+informTime[x]; 28 que.push(v); 29 } 30 } 31 return ans; 32 } 33 };
5355. T-second position after the frog
1 const int maxn=1e4; 2 class Solution { 3 public: 4 double frogPosition(int n, vector<vector<int>>& edges, int t, int target) { 5 if(edges.size()==0)return 1.0; 6 vector<int>edge[maxn]; 7 double dist[maxn]; 8 int cnt[n+1][2]; 9 for(int i=1;i<=n;i++){ 10 dist[i]=0.0; 11 //cnt[i].clear(); 12 cnt[i][0]=0; 13 cnt[i][1]=0; 14 } 15 for(int i=0;i<edges.size();i++){ 16 if(edges[i][0]>edges[i][1])swap(edges[i][0],edges[i][1]); 17 edge[edges[i][0]].push_back(edges[i][1]); 18 //edge[edges[i][1]].push_back(edges[i][0]); 19 } 20 21 queue<int>que; 22 que.push(1); 23 dist[1]=1.0; 24 int start=0;int end=1; 25 int level=1; 26 while(!que.empty()){ 27 start++; 28 int min_num=level; 29 30 int x=que.front(); 31 que.pop(); 32 int num=edge[x].size(); 33 double percent=1.0/(double)num; 34 for(int i=0;i<num;i++){ 35 int v=edge[x][i]; 36 if(edge[v].size()==0)cnt[v][1]=1; 37 cnt[v][0]=min_num; 38 dist[v]=(double)percent*dist[x]; 39 que.push(v); 40 } 41 //cout<<level<<endl; 42 if(start==end){ 43 level++; 44 start=0; 45 end=que.size(); 46 } 47 } 48 //cout<<cnt[target][0]<<" "<<level<<" "<<cnt[target][1]<<endl; 49 if((cnt[target][1]==0&&t==cnt[target][0])||(cnt[target][1]&&t>=cnt[target][0])){ 50 // cout<<"ok"<<endl; 51 // cout<<dist[target]<<endl; 52 return dist[target]; 53 }else{ 54 return 0.0; 55 } 56 57 } 58 };