pranp_24's blog

By pranp_24, history, 11 months ago, In English

I was just working on a problem and I can't understand why one of my solutions passed and the other didn't.

I have two loops that are the same complexity. Since they are the same complexity, it should simplify to O(N)+O(N)=O(2N) which just becomes O(N). When I take out one of the loops, my solution passes, but when I keep them both in I get TLE. I don't understand this since they should simplify to the same complexity. Can someone please help?

Not accepted solution:

while(number>0){
  if(number % 2==0){
       stack.add(1);
  }
  else{
     stack.add(2);
  }
  number/=2;
}

int originalsize= stack.size();
for (int i = 0; i < originalsize; i++) {
   System.out.print(stack.pop()+ " ");
}

Accepted solution:

String ans="";
while(number>0){
  if(number % 2==0){
       ans="1 "+ans;
  }
  else{
     ans="2 "+ans;
   }
  number/=2;
}
System.out.println(ans);

Problem link: https://codeforces.com/contest/1810/problem/B

Accepted Solution: https://codeforces.com/contest/1810/submission/207920289

Not accepted Solution: https://codeforces.com/contest/1810/submission/207919439

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it

By pranp_24, history, 15 months ago, In English

So, I just got hacked on this problem from the recent Div4 contest for problem G1 and I still don't know why. I got TLE but there is nothing in my code that could've caused it. Can someone please help?

Submission Link: https://codeforces.com/contest/1791/submission/192492521 Problem Link: https://codeforces.com/contest/1791/problem/G1

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it