반응형
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
Tags
- ssh
- MAPF
- terraform
- Go-lang
- AWS
- DevOps
- leetcode
- 회고
- 티스토리챌린지
- AWS 비용 절감
- 오블완
- 14일 공부
- github
- Playwright
- 디자인 패턴
- docker
- PostgreSQL
- 신혼 여행
- Til
- 생성 패턴
- Rust
- 구조 패턴
- amazon ecs
- 청첩장 모임
- 실용주의 프로그래머
- 커머스
- 경로 계획 알고리즘
- study
- 지표
- 논문 정리
Archives
- Today
- Total
밤 늦게까지 여는 카페
[leetcode] 2257. Count Unguarded Cells in the Grid 본문
반응형
안녕하세요. 오늘은 leetcode의 2257. Count Unguarded Cells in the Grid 문제를 풀어봤습니다.
1. 문제
이 문제에서는 두 수 m, n과 좌표로 이뤄진 두 리스트 guards, walls가 주어집니다.
- m, n은 격자 지도의 세로와 가로를 의미합니다.

- guards는 guard들의 좌표 리스트입니다. guard들은 자신의 좌표에서 상하좌우 방향으로 탐색할 수 있습니다.

- walls는 wall들의 좌표 리스트입니다. wall은 guard들의 탐색을 막습니다.

문제에서는 guard들이 탐색하지 못하는 칸의 개수를 구해야 합니다.
(1, 1)에 guard가 있고 (2, 1)에 wall이 있는 위의 그림에서 guard들이 탐색하지 못하는 칸의 개수는 7개입니다!
2. 풀이
제가 시도한 방법은 단순합니다.
모든 guard들에 대해서 guard를 만나거나 wall을 만날 때까지 상하좌우 방향으로 계속해서 탐색하는 것이죠.
- 대신에 중복 계산을 막기 위해서 python의 dictionary 자료형을 이용했습니다.
하지만 너무 단순했던 걸까요? runtime 효율이 극악입니다 ㅜㅠ

이제 더 개선된 풀이를 찾아봐야겠죠?
+코드
더보기
class Solution:
def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:
num_of_unguarded_cells = m * n
walls_dict = {}
visited = {}
for wall in walls:
visited[(wall[0], wall[1])] = True
walls_dict[(wall[0], wall[1])] = True
for guard in guards:
visited[(guard[0], guard[1])] = True
walls_dict[(guard[0], guard[1])] = True
for guard in guards:
check_up(m, n, guard, walls_dict, visited)
check_down(m, n, guard, walls_dict, visited)
check_left(m, n, guard, walls_dict, visited)
check_right(m, n, guard, walls_dict, visited)
return num_of_unguarded_cells - len(visited)
def check_up(num_of_row, num_of_cols, guard, walls_dict, visited):
[row_of_guard, col_of_guard] = guard
for row in range(row_of_guard-1, -1, -1):
visited[(row, col_of_guard)] = True
if (row, col_of_guard) in walls_dict:
break
def check_down(num_of_row, num_of_cols, guard, walls_dict, visited):
[row_of_guard, col_of_guard] = guard
for row in range(row_of_guard+1, num_of_row):
visited[(row, col_of_guard)] = True
if (row, col_of_guard) in walls_dict:
break
def check_left(num_of_row, num_of_cols, guard, walls_dict, visited):
[row_of_guard, col_of_guard] = guard
for col in range(col_of_guard-1, -1, -1):
visited[(row_of_guard, col)] = True
if (row_of_guard, col) in walls_dict:
break
def check_right(num_of_row, num_of_cols, guard, walls_dict, visited):
[row_of_guard, col_of_guard] = guard
for col in range(col_of_guard+1, num_of_cols):
visited[(row_of_guard, col)] = True
if (row_of_guard, col) in walls_dict:
break
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
| [leetcode] 2257. Count Unguarded Cells in the Grid #3 (2) | 2024.11.29 |
|---|---|
| [leetcode] 2257. Count Unguarded Cells in the Grid #2 (1) | 2024.11.27 |
| [leetcode] 2516. Take K of Each Character From Left and Right #2 (0) | 2024.11.22 |
| [leetcode] 2516. Take K of Each Character From Left and Right (2) | 2024.11.21 |
| [leetcode] 1652. Defuse the Bomb (2) | 2024.11.19 |