Skip to content

Commit 5a4b8b4

Browse files
Solved 1568 (finally!)
1 parent 707b822 commit 5a4b8b4

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

problems/1568/jeremymanning.md

+107-1
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,112 @@ class Solution:
447447

448448
- Ugh...time limit exceeded when `grid = [[1,1,1,1,1,1,1,1,1,1,1,0,1],[1,1,1,1,1,1,1,1,1,0,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,0,1,1,1,1,1,1,1],[0,1,1,0,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,0,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,0],[1,0,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,0,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,0,1,1,1,1,1],[1,1,1,1,1,0,1,1,1,0,1,1,1],[0,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,0,1,1,1,1,1,1,1]]`
449449
- And even *testing* that one example doesn't work (runs out of time in the manual testing system too)
450-
- I'm going to come back to this tomorrow...
451450

451+
## Refining the problem (again)
452+
- There are a few edge cases to keep in mind; we could check for these at the start:
453+
- If there are *no* 1's, return 0 (nothing to be done)
454+
- If there is just a *single* 1, return 1 (convert that one cell to a 0)
455+
- The most inefficient piece of our solution is the `one_island` function. We end up going through (on the order of) every cell three times. We could do this more efficiently:
456+
```python
457+
def one_island(grid):
458+
visited = [[False for _ in grid[0]] for _ in grid]
459+
count = 0
460+
461+
def bfs(i, j):
462+
queue = [(i, j)]
463+
visited[i][j] = True
464+
while len(queue) > 0:
465+
x, y = queue.pop(0)
466+
for a, b in neighbors(x, y):
467+
if not visited[a][b]:
468+
visited[a][b] = True
469+
queue.append((a, b))
470+
471+
for i in range(len(grid)):
472+
for j in range(len(grid[0])):
473+
if grid[i][j] == 1 and not visited[i][j]:
474+
bfs(i, j) # visit everywhere connected with (i, j)
475+
count += 1
476+
if count > 1:
477+
return False
478+
479+
return count == 1
480+
```
481+
- This avoids two additional passes through the full grid, although the time complexity is unchanged. Let's put it all together and see if it works (and we can also check those edge cases initially in case that helps too):
482+
```python
483+
class Solution:
484+
def minDays(self, grid: List[List[int]]) -> int:
485+
def neighbors(i, j):
486+
x = []
487+
if i > 0 and grid[i - 1][j] == 1:
488+
x.append((i - 1, j))
489+
490+
if i < len(grid) - 1 and grid[i + 1][j] == 1:
491+
x.append((i + 1, j))
492+
493+
if j > 0 and grid[i][j - 1] == 1:
494+
x.append((i, j - 1))
495+
496+
if j < len(grid[0]) - 1 and grid[i][j + 1] == 1:
497+
x.append((i, j + 1))
498+
return x
499+
500+
def one_island(grid):
501+
visited = [[False for _ in grid[0]] for _ in grid]
502+
count = 0
503+
504+
def bfs(i, j):
505+
queue = [(i, j)]
506+
visited[i][j] = True
507+
while len(queue) > 0:
508+
x, y = queue.pop(0)
509+
for a, b in neighbors(x, y):
510+
if not visited[a][b]:
511+
visited[a][b] = True
512+
queue.append((a, b))
513+
514+
for i in range(len(grid)):
515+
for j in range(len(grid[0])):
516+
if grid[i][j] == 1 and not visited[i][j]:
517+
bfs(i, j) # visit everywhere connected with (i, j)
518+
count += 1
519+
if count > 1:
520+
return False
521+
522+
return count == 1
523+
524+
def single_fix(grid):
525+
for i in range(len(grid)):
526+
for j in range(len(grid[0])):
527+
if grid[i][j] == 1:
528+
# try removing this cell
529+
grid[i][j] = 0
530+
if not one_island(grid):
531+
grid[i][j] = 1
532+
return True
533+
grid[i][j] = 1
534+
return False
535+
536+
# handle edge cases
537+
n_ones = 0
538+
for i in range(len(grid)):
539+
for j in range(len(grid[0])):
540+
n_ones += grid[i][j]
541+
if n_ones == 0:
542+
return 0
543+
elif n_ones == 1:
544+
return 1
545+
546+
if not one_island(grid):
547+
return 0
548+
elif single_fix(grid):
549+
return 1
550+
else:
551+
return 2
552+
```
553+
- Ok...test cases (still) pass...
554+
- I'll submit again... 🤞
555+
556+
![Screenshot 2024-08-11 at 3 16 52 PM](https://github.com/user-attachments/assets/6f21fa4a-2a6b-411d-b331-6eacc285a40c)
452557

558+
Phew!

0 commit comments

Comments
 (0)