Here is Micko's code:
Excellent job as this produces the correct result for every test case I tried. I admit it took awhile to understand the logic behind how it works but essentially it does more or less exactly what my code does just in a different manner. It starts with a full string of white and gold pearls, and as it loops through it swaps the first 'k' white pearl with the first unmoved gold pearl.Code:class Micko { enum { w = 0, g }; struct element { int num; unsigned int check :1; }; public: string originalNecklace(int, int, int); int find_first(element*, int, int); }; string Micko::originalNecklace(int W, int G, int K) { int i; element* e = new element[W + G]; for (i = 0; i < W; i++) { e[i].num = w; e[i].check = 0; } for (i = W; i < W + G; i++) { e[i].num = g; e[i].check = 0; } /* Now initialization is finished*/ /*************************************/ /* Now starting hard work*/ int start = 0; int end; int end_loop = 0; while (end_loop != G) { i = start; end = start + K; while(i < end) { if (i == W + G) { i %= (W + G); end -= (W + G); } if (e[i].check == 1) end++; i++; } start = i; i--; if ((e[i].num == g) && (e[i].check == 0)) { e[i].check = 1; end_loop++; } else { int index = find_first(e, i + 1, W + G -1); swap(e[i].num, e[index].num); e[i].check = 1; end_loop++; } } string s; for (i = 0; i < W + G; i++) { if(e[i].num == g) s += "g"; else s +="w"; } delete [] e; return s; } int Micko::find_first(Micko::element* e, int l, int r) { for (int m = l; m <= r; m++) { if ((e[m].num == g) && (e[m].check == 0)) return m; } return 0; }
My score: 10 points for correct answers + 1 snazzy point for solving in a unique way for a grand total of 11 points.



LinkBack URL
About LinkBacks



