Skip to content

Bug Report for surrounded-regions #5553

@khuzhokov

Description

@khuzhokov

Bug Report for https://neetcode.io/problems/surrounded-regions

run code endpoint timeouts when you press 'run' instead of 'submit' with this code in this problem

func solve(board [][]byte) {
	rows, cols := len(board), len(board[0])
	q := [][2]int{}

	add := func(r, c int) {
		pos := [2]int{r, c}
		if r < 0 || c < 0 || r >= rows || c >= cols ||  board[r][c] != 'O' {
			return
		}
		board[r][c] = 'T'
		q = append(q, pos)
	}

	for r := range rows {
		add(r, 0)
		add(r, cols-1)
	}

	for c := range cols {
		add(0, c)
		add(rows-1, c)
	}

	for len(q) > 0 {
		f := q[0]
		q = q[1:]
		r, c := f[0], f[1]
		add(r - 1, c)
		add(r + 1, c)
		add(r, c - 1)
		add(r, c + 1)
	}

	for r := range rows {
		for c := range cols {
			if board[r][c] == 'O' {
				board[r][c] = 'X'
			} else if board[r][c] == 'T' {
				board[r][c] = 'O'
			}
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions