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.
// passed all the test cases and follows the train of thought//laid out by the problem setter.//may not be optimal , switching context between multiple //languages is a double edged sword//it took a whole day to train on some aspects of jsfunctionpageCount(n,p){constgenerate_pages=(n)=>{constarr=[]for(leti=1;i<=n;i++){arr.push(i)}returnarr;}constget_pages_for_book=(data)=>{letleftrightpages=[];leftrightpages.push([0,data[0]]);for(leti=1;i<=data.length;i+=2){if(i+1<data.length){leftrightpages.push([data[i],data[i+1]]);}else{leftrightpages.push([0,data[i]||0]);}}returnleftrightpages;}constwalk_foward=(data,target)=>{for(leti=0;i<=data.length;i++){if(data[i].includes(target)){returni;}}return0;}constwalk_backward=(data,target)=>{letk=0;for(letj=data.length-1;j>=0;j--){if(data[j].includes(target)){returnk;}k=k+1;}return0;}letpages=generate_pages(n);lettuples=get_pages_for_book(pages);constfiltered=tuples.filter(pair=>!(pair[0]===0&&pair[1]===0));letforward=walk_foward(filtered,p);letbackward=walk_backward(filtered,p);returnMath.min(forward,backward);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Drawing Book
You are viewing a single comment's thread. Return to all comments →