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.
Can someone help me to find out a more optimal solution? I am storing the result in a map to reduce the time but it still times out on all test cases other then 1 and 2.
#include<bits/stdc++.h>usingnamespacestd;map<pair<string,int>,int>winners;//0 mine//1 opponentintwinner(charpin[],intn,intturn){if(winners[pair<string,int>(string(pin),turn)]){returnwinners[pair<string,int>(string(pin),turn)];}if(winners[pair<string,int>(string(pin),((turn==1)?2:1))]){return((winners[pair<string,int>(string(pin),((turn==1)?2:1))]==1)?2:1);}intflag=0;for(inti=0;i<n;i++){if(pin[i]=='I'){flag=1;break;}}if(!flag){return((turn==1)?2:1);}for(inti=0;i<n;i++){inttemp=0;if(pin[i]=='I'){pin[i]='X';temp=winner(pin,n,((turn==1)?2:1));if(temp==turn){pin[i]='I';winners[pair<string,int>(string(pin),turn)]=temp;returntemp;}pin[i]='I';}temp=0;if(i<(n-1)){if((pin[i]=='I')&&(pin[i+1]=='I')){pin[i]='X';pin[i+1]='X';temp=winner(pin,n,((turn==1)?2:1));if(temp==turn){pin[i]='I';pin[i+1]='I';winners[pair<string,int>(string(pin),turn)]=temp;returntemp;}pin[i]='I';pin[i+1]='I';}}}winners[pair<string,int>(string(pin),turn)]=((turn==1)?2:1);return((turn==1)?2:1);}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intq;cin>>q;for(inti=0;i<q;i++){intn;cin>>n;charpin[n];for(intj=0;j<n;j++){cin>>pin[j];}inta;if(winners[pair<string,int>(string(pin),1)]){a=winners[pair<string,int>(string(pin),1)];}elseif(winners[pair<string,int>(string(pin),2)]){a=((winners[pair<string,int>(string(pin),2)]==1)?2:1);}elsea=winner(pin,n,1);if(a==1){cout<<"WIN"<<endl;}else{cout<<"LOSE"<<endl;}}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bowling Pins
You are viewing a single comment's thread. Return to all comments →
Can someone help me to find out a more optimal solution? I am storing the result in a map to reduce the time but it still times out on all test cases other then 1 and 2.