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.
My answer with Typescript,
Has runtime error with few very very large test 100.000+ that idk where cause 50kb limit test
functionanswerQuery(l:number,r:number):number{letmemo_factorial=newMap<bigint,bigint>([[0n,1n],[1n,1n]])functionfactorial(n:bigint):bigint{if(!memo_factorial.has(n)){leti_=Array.from(memo_factorial.keys()).pop()||1n;letr_=memo_factorial.get(i_)!for(leti=i_+1n;i<=n;i++){r_*=imemo_factorial.set(i,r_)}}returnmemo_factorial.get(n)!;}// 0. get the substring from [l] and [r], get [frequency]consts=str.substring(l-1,r)constfrequency=newMap<string,number>()for(letcofs)frequency.set(c,(frequency.get(c)||0)+1)/** * factorial(p_alls) * m * ------------------------------- (modulo 10^9 + 7) * factorial(p*...) * * with: * + [m]: numbers of character that odd, mean it can be middle * + [p]: colection of character that can be appears in 2 side of palidrome *//** * Update 1: Number exceeded Number.MAX_SAFE_INTEGER -> using BigInt * Update 2: Maximum call stack exceeded -> changes [factorial] to use for...loop insteads of recursive * Update 3: Some test timeout -> try memo * Update 4: Some test with 100.000 has runtime error, idk where cause 50kb limit custom test */// 1. calculate prepare for formularletp_alls=factorial(BigInt(Array.from(frequency.values()).reduce((_,v)=>_+Math.floor(v/2),0)))letp_each=Array.from(frequency.values()).reduce((_,v)=>_*factorial(BigInt(Math.floor(v/2))),1n)letm=BigInt(Math.max(Array.from(frequency.values()).reduce((_,v)=>_+v%2,0),1))// 2. apply formularreturnNumber((m*p_alls/p_each)%BigInt(10**9+7))}
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 →
My answer with Typescript, Has runtime error with few very very large test 100.000+ that idk where cause 50kb limit test