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.
The only trick here is to cache the frequency map, since you are going to use it for every single test in the test-range. Do not bother caching modinverse, etc. The real cost is the string manipulation.
Do something like this in your initialize function
And of course, calculate the difference in frequencies...
intanswerQuery(intfirst,intlast){autolen=last-first+1;if(len==1)return1;std::vector<int>freqs=s_cumulativeFreq[last];// Compute the frequencies in the range [l, r){autoleftIter=next(s_cumulativeFreq.begin(),first-1);// I want to remove the entries BEFORE 'first'transform(freqs.begin(),freqs.end(),leftIter->begin(),freqs.begin(),std::minus<int>());// Compute the frequencies in the range [l, r]}automiddleChars=std::count_if(freqs.begin(),freqs.end(),[](intf){returnf&1;});// Now I divide by 2. I only care about the left side of the palindromefor_each(freqs.begin(),freqs.end(),[](auto&f){f>>=1;});freqs.erase(std::remove(freqs.begin(),freqs.end(),0),freqs.end());// Total number of chars to permutate...autopalindromeChars=accumulate(freqs.begin(),freqs.end(),0,std::plus<int>());...Youcanfilltherest...
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Palindromes
You are viewing a single comment's thread. Return to all comments →
The only trick here is to cache the frequency map, since you are going to use it for every single test in the test-range. Do not bother caching modinverse, etc. The real cost is the string manipulation. Do something like this in your initialize function
And of course, calculate the difference in frequencies...