We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Journey to the Moon
Journey to the Moon
Sort by
recency
|
511 Discussions
|
Please Login in order to post a comment
For python code, test case 11, if you use summation over and over for calculating the final results, you could use cummulative sum to avoid it. it will help you to speed up.
Runtime error on #11 testcase others are successfully executed
int parent[100001]; int Size[100001];
void make(int n){ for(int i=0;i
int find(int vertex){ if(parent[vertex]==vertex)return vertex; return parent[vertex]=find(parent[vertex]); }
void Union(int a,int b){ a=find(a); b=find(b); if(a==b)return ; if(Size[b]>Size[a])swap(a,b); parent[b]=a; Size[a]+=Size[b]; }
int journeyToMoon(int n, vector> astronaut) { make(n);
I don't know how anyone solved this in Python without special handling for Test Case #11, in which there are 100000 single-astronaut countries. Look at the test case data! Once I computed the "disjoint sets" using normal graph algorithms (this is actually just an array of subgraph sizes, since it doesn't matter which nodes go into a subgraph), I could calculate possible astronaut pairs from single-astronaut countries as
number of singletons * (number of singletons - 1) // 2
, and so on.Python solution based on : * Adjancency List * Breadth First Search * Combination for pairing with each other ungrouped
def journeyToMoon(n, astronaut):
The output for one of the test cases do not fit into int range for c++.
ans is 4999949998 but int represantation is not possible, which is return type of function