You are viewing a single comment's thread. Return to all comments →
If you sort the both arrays first, then you get a much faster proccessing time. Consider this:
static void Main(String[] args) { string[] tokens_m = Console.ReadLine().Split(' '); int m = Convert.ToInt32(tokens_m[0]); int n = Convert.ToInt32(tokens_m[1]); string[] magazine = Console.ReadLine().Split(' '); string[] ransom = Console.ReadLine().Split(' '); Array.Sort(ransom); Array.Sort(magazine); bool ransompossible=true; for(int i=0; i< n; i++){ bool wordfound = false; for (int j=0;j<m;j++){ if (magazine[j]==ransom[i]){ magazine[j] = ""; wordfound = true; break; } } if (!wordfound) { ransompossible=false; break; } } if (ransompossible){ Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } }
if you comment out the Array.Sort lines you will get a timeout for 3000 x 3000 word test case.
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
If you sort the both arrays first, then you get a much faster proccessing time. Consider this:
if you comment out the Array.Sort lines you will get a timeout for 3000 x 3000 word test case.