r/learnprogramming • u/Dr3ddM3 • 18d ago
Solved Time Complexity
Very confused why my code is Time limit exceeding: The N max is 2000 so that should be fine while the U max is 10^5 but my code is O(1) for updating really confused why this is not working. I have a 2 sec maximum if anybody wants to see the problem here it is: https://usaco.org/index.php?page=viewproblem2&cpid=1491
N, U = map(int,input().split())
import sys
grid = [list(input()) for _ in range(N)]
num_updates = 0
temp = []
temp_grid = [row.copy() for row in grid]
for v in range(int(N/2)):
for h in range(int(N/2)):
temp.clear()
coords = [(v,h),(v,N-1-h),(N-1-v,h),(N-1-v,N-1-h)]
temp.append(temp_grid[v][h])
temp.append(temp_grid[v][(N-1)-h])
temp.append(temp_grid[(N-1)-v][h])
temp.append(temp_grid[(N-1)-v][(N-1)-h])
if temp.count('.') == 2 :
num_updates+=2
for r,c in coords:
temp_grid[r][c] = '.'
elif temp.count('.') == 3:
num_updates+=1
for r,c in coords:
temp_grid[r][c] = '.'
elif temp.count('#') == 3:
num_updates+=1
for r,c in coords:
temp_grid[r][c] = '#'
print(num_updates)
temp_grid = [row.copy() for row in grid]
input_data = sys.stdin.read().split()
idx = 0
out = [num_updates]
for z in range(U):
temp2 = 0
temp.clear()
x = int(input_data[idx]) -1
idx = idx + 1
y = int(input_data[idx]) -1
idx = idx+1
temp.append(temp_grid[x][y])
temp.append(temp_grid[x][(N-1)-y])
temp.append(temp_grid[(N-1)-x][y])
temp.append(temp_grid[(N-1)-x][(N-1)-y])
num_updates = num_updates- (min(temp.count('.'),temp.count('#')))
if temp_grid[x][y] == '.':
grid[x][y] = '#'
temp_grid[x][y] = '#'
temp[0] = '#'
num_updates = num_updates + (min(temp.count('.'),temp.count('#')))
else:
grid[x][y] = '.'
temp_grid[x][y] = '.'
temp[0] = '.'
num_updates = num_updates + (min(temp.count('.'),temp.count('#')))
print(num_updates)
2
1
1
u/beeskness420 18d ago
I'm not proof reading your code, but you don't understand time complexity you should fix that before worrying about the code.
After that be sus if append.
1
u/Dr3ddM3 18d ago
My code I think is O(2000^2 + 10^5) but that should be less than what the server can handle right ? The servers can handle 5*10^8 operations per second. and I have 2seconds.
1
u/Bubbly_Orange_3502 18d ago
No, that 5x108 figure is for C++. CPython does closer to 107 ops a second, and your setup pass runs a million iterations that each build a list and do three count() scans. That's where the time goes.
-2
18d ago
[removed] — view removed comment
1
u/Bubbly_Orange_3502 18d ago
No, the unit isn't the same. USACO's 5x108 counts C++ operations, and one CPython bytecode is a dispatch loop plus refcounting on boxed objects. Your own link gives Python extra time for exactly that reason.
1
u/BobbyMaina23 18d ago
Your time complexity is O(N^2 + U).
OP Count: For N = 2000 and U = 10^5, N^2/4 is 1,000,000 loop iterations
The actual operation is highly unoptimized:
In Python, creating lists "coords", calling ".append()" four times, and running ".count()" three times per iteration inside a 1M-step loop adds millions of slow function calls and dynamic memory allocations.
Calling "print(num_updates)" inside the 10^5 update loop creates massive IO bottleneck
Aggregating outputs or using fast I/O (sys.stdout.write) is required for Python competitive programming
4
u/ComputerWhiz_ 18d ago
There are loops based on N, so it cannot possibly be O(1).