- 
                Notifications
    
You must be signed in to change notification settings  - Fork 380
 
Closed
Labels
Description
Your LeetCode username
hqztrue
Category of the bug
- Question
 - Solution
 - Language
 - Missing Test Cases
 
Description of the bug
An accepted solution gets TLE on the following testcase:
1.txt
Code you used for Submit/Run operation
class Solution:
	def minimumOperations(self, grid: List[List[int]]) -> int:
		edges = collections.defaultdict(list)
		n,m = len(grid),len(grid[0])
		cnt = 0
		for i in range(n):
			for j in range(m):
				if grid[i][j] == 0:
					continue
				cnt += 1
				
				if j - 1 >= 0 and grid[i][j - 1] == 1:
					edges[i * m + j].append(i * m + j - 1)
				if j + 1 < m and grid[i][j + 1] == 1:
					edges[i * m + j].append(i * m + j + 1)
				
				if i + 1 < n and grid[i + 1][j] == 1:
					edges[i * m + j].append((i + 1) * m + j)
				if i - 1 >= 0 and grid[i - 1][j] == 1:
					edges[i * m + j].append((i - 1) * m + j)
				
				
				'''if i - 1 >= 0 and grid[i - 1][j] == 1:
					edges[i * m + j].append((i - 1) * m + j)
					edges[(i - 1) * m + j].append(i * m + j)
				if j - 1 >= 0 and grid[i][j - 1] == 1:
					edges[i * m + j].append(i * m + j - 1)
					edges[i * m + j - 1].append(i * m + j)'''
		vis = set()
		d = 0
		def match(i):
			nonlocal edges,vis,d
			d+=1
			if i in vis:
				return False
			vis.add(i)
			for j in edges[i]:
				if j not in link or match(link[j]):
					link[j] = i
					link[i] = j
					return True
			return False
			
		link = {}
		ans = 0
		s=0
		for i in edges:
			if i not in link:
			   vis.clear()
			   d=0
			   if match(i):
			     ans +=1
			   s+=d
			   if (d>10000):print(i,d)
		print('s=',s)
		return ans
Language used for code
Python3
Expected behavior
The code gets TLE.