Блог пользователя object_oriented_program

Автор object_oriented_program, 11 лет назад, По-английски

I tried this sum and implemented a O(n2) solution, which should pass given n < 2000 , but it doesnt.. any explanations why ?

solution

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Range is working slow. So for solution on python requre more advanced approaches. Also you can reduce search diapason, enough iterate one coordinate quartal for each vertex.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Greetings. Your solution is 2000**2 + 2000*2 which is 8*10^6 operations. Python can hardly make more than 7*10^6 operations per second, so your solution must be just above the threshold of 1 second.

»
11 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

You can simply iterate over  < 0, a >  not  <  - a, a >  and if you get match, add (i, j) and (i,  - j) Also, count once and keep a * a, i * iandb * b in separate variables.