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.
I had the same issue in C#, but found that the memory limit is even stricter (~300mb). I ended up writing this class to store bools instead, hopefully someone will find it helpful:
publicclassBoolMatrix{privateDictionary<int,uint[]>matrix;privateintnumCells;publicBoolMatrix(intsize){// each 'cell' is effectively used as an array for 32 boolsnumCells=(int)Math.Ceiling((double)size/32);matrix=newDictionary<int,uint[]>();}publicvoidSetTrue(introw,intcol){row-=1;col-=1;if(!matrix.ContainsKey(row)){matrix[row]=newuint[numCells];}matrix[row][col/32]|=(uint)(1<<(col%32));}publiclongCountTrues(){longtrues=0;foreach(varrowinmatrix.Values){for(inti=0;i<row.Length;i++){trues+=CountBits(row[i]);}}returntrues;}// Kernighan's algorithmprivateintCountBits(uintn){intcount=0;while(n>0){n&=n-1;count+=1;}returncount;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #29: Distinct powers
You are viewing a single comment's thread. Return to all comments →
I had the same issue in C#, but found that the memory limit is even stricter (~300mb). I ended up writing this class to store bools instead, hopefully someone will find it helpful: