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.
Java solultion in O(n). Similar logic to what others have expressed: we need to keep track of the previous number of nodes, the previous sum of all paths, the previous sum of all nodes to a corner node, and the previous diagonal path sum (aka. the longest path). We these values, we can find the values of the new city size with a little bit of combinatorics. The trickiest part is dealing with the modulo imo.
intmod=1000000007;if(A.size()==1){return29*(A.get(0));}longprevAll=29*A.get(0);longprevAllToCorner=11*A.get(0);longprevCornerToCorner=3*A.get(0);longprevNumNodes=6;for(ints=1;s<A.size();s++){intcurrWeight=A.get(s);// Start with sum between inner two nodes and between previous nodeslongcurrSum=((4*prevAll)%mod+currWeight)%mod;// Inner Two Nodes to Previous Nodes (or quadrants) (times four)longcloserInner=((currWeight*prevNumNodes)%mod+prevAllToCorner)%mod;longfartherInner=((2*currWeight*prevNumNodes)%mod+prevAllToCorner)%mod;longtotalInnerToQuadrants=(4*(closerInner+fartherInner)%mod)%mod;currSum=(currSum+totalInnerToQuadrants)%mod;// Previous Nodes to Previous NodeslongthreeQuadrantNodes=(3*prevNumNodes)%mod;longoneCornerUsage=(prevAllToCorner*threeQuadrantNodes)%mod;longtotalCornerUsage=(4*oneCornerUsage)%mod;currSum=(currSum+totalCornerUsage)%mod;// Total inner edge path usage (aka. new inner edges) between previous nodeslongsqrNodes=(prevNumNodes*prevNumNodes)%mod;longinnerBetween2=(2*(currWeight*sqrNodes)%mod)%mod;longinnerBetween3=(3*(currWeight*sqrNodes)%mod)%mod;longtotalInBetween=((2*innerBetween2)%mod+(4*innerBetween3)%mod)%mod;currSum=(currSum+totalInBetween)%mod;// Find new sum of all nodes to a corner nodelongcurrAllToCorner=prevAllToCorner;// One quad uses two edge inner pathlongcloserQuad=(prevAllToCorner+prevNumNodes*(2*currWeight))%mod;currAllToCorner=(currAllToCorner+closerQuad)%mod;// Two quads use three edge inner pathlongfurtherQuads=(prevAllToCorner+prevNumNodes*(3*currWeight))%mod;currAllToCorner=(currAllToCorner+(2*furtherQuads)%mod)%mod;// Inner nodes to upper cornerlonginnerNodesToCorner=3*currWeight;currAllToCorner=(currAllToCorner+innerNodesToCorner)%mod;// How many times the previous corner to corner path will be usedlongcornerToCornerUsage=((threeQuadrantNodes+2)%mod*prevCornerToCorner)%mod;currAllToCorner=(currAllToCorner+cornerToCornerUsage)%mod;// Update DP valuesprevNumNodes=(4*prevNumNodes+2)%mod;prevCornerToCorner=(2*prevCornerToCorner+3*currWeight)%mod;prevAll=currSum;prevAllToCorner=currAllToCorner;}return(int)prevAll;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
HackerRank City
You are viewing a single comment's thread. Return to all comments →
Java solultion in O(n). Similar logic to what others have expressed: we need to keep track of the previous number of nodes, the previous sum of all paths, the previous sum of all nodes to a corner node, and the previous diagonal path sum (aka. the longest path). We these values, we can find the values of the new city size with a little bit of combinatorics. The trickiest part is dealing with the modulo imo.