tag:blogger.com,1999:blog-2597841815280998418.post173587499968421526..comments2024-03-12T06:45:01.935+01:00Comments on This Thread: Is it a permutation?Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-2597841815280998418.post-1845067369791224172014-07-29T21:59:14.096+02:002014-07-29T21:59:14.096+02:00Thank you for contributing, Ajeet.Thank you for contributing, Ajeet.Mannyhttps://www.blogger.com/profile/07393063644320426727noreply@blogger.comtag:blogger.com,1999:blog-2597841815280998418.post-65047006315875778362014-07-28T05:34:48.989+02:002014-07-28T05:34:48.989+02:00PHP 100/100 for PermCheck Codility : Ajeet Singh
...PHP 100/100 for PermCheck Codility : Ajeet Singh<br /><br />function solution($A) {<br /> $count_of_elements = array_fill(0, count($A)+1, null);<br /> for($i=0; $i < count($A); $i++ ){<br /><br /> $count_of_elements[$A[$i]] += 1;<br /><br /> if($A[$i] > count($A) || $A[$i] <= 0 || $count_of_elements[$A[$i]] > 1)<br /> return 0;<br /> <br /> }<Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2597841815280998418.post-79917084842184699472013-11-18T00:50:20.105+01:002013-11-18T00:50:20.105+01:00Absolutely right. The final check on flags doesn&#...Absolutely right. The final check on flags doesn't make any sense anymore.Mannyhttps://www.blogger.com/profile/07393063644320426727noreply@blogger.comtag:blogger.com,1999:blog-2597841815280998418.post-31056824926133549992013-11-17T23:35:55.169+01:002013-11-17T23:35:55.169+01:00If you have tested for duplicates and have checked...If you have tested for duplicates and have checked that no values larger then N exist, I think you can also remove the last line and simply return true.Anonymoushttps://www.blogger.com/profile/14158764176533262082noreply@blogger.comtag:blogger.com,1999:blog-2597841815280998418.post-47692642326249900042013-11-17T21:41:14.411+01:002013-11-17T21:41:14.411+01:00Yes, Karsten, thank you. You are right, testing fo...Yes, Karsten, thank you. You are right, testing for duplicates is cheap, and it could save substantial computational time. I have modified the code following your suggestion.Mannyhttps://www.blogger.com/profile/07393063644320426727noreply@blogger.comtag:blogger.com,1999:blog-2597841815280998418.post-77646557321192564262013-11-17T21:25:58.062+01:002013-11-17T21:25:58.062+01:00Sorry, i mean buffer[ value ] == true. If this is ...Sorry, i mean buffer[ value ] == true. If this is the case you can return false.Anonymoushttps://www.blogger.com/profile/14158764176533262082noreply@blogger.comtag:blogger.com,1999:blog-2597841815280998418.post-40245731386760640672013-11-17T20:59:56.853+01:002013-11-17T20:59:56.853+01:00You can speed up your algorithm if you check in li...You can speed up your algorithm if you check in line 8 if buffer[3] is already true. In this case you have a duplicate and you can return true.Anonymoushttps://www.blogger.com/profile/14158764176533262082noreply@blogger.com