반응형
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 |
Tags
- AWS
- PostgreSQL
- DevOps
- 청첩장 모임
- docker
- 오블완
- Til
- 디자인 패턴
- 논문 정리
- 구조 패턴
- 지표
- MAPF
- Go-lang
- amazon ecs
- terraform
- 커머스
- study
- 경로 계획 알고리즘
- 신혼 여행
- 티스토리챌린지
- Rust
- 회고
- github
- leetcode
- 실용주의 프로그래머
- AWS 비용 절감
- Playwright
- 생성 패턴
- 14일 공부
- ssh
Archives
- Today
- Total
밤 늦게까지 여는 카페
[leetcode] 2257. Count Unguarded Cells in the Grid #2 본문
반응형
안녕하세요. 오늘은 이전에 풀었던 2257. Count Unguarded Cells in the Grid 문제를 다시 풀어보겠습니다.

1. 개선된 문제 풀이
이전에 풀었던 방법의 시간복잡도는 O(max(m, n) * guard 수) 입니다.
그런데 'max(m, n) * guard 수'는 최대 10^9까지 커질 수 있습니다!
그래서 알고리즘의 속도가 느린 것이었죠.
- 격자지도의 모든 점을 확인하는 것보다는 모든 guard에 대해서만 확인하는 것이 효율적이라고 생각했지만 문제 조건이 그렇지 않았습니다...
- guard의 수만큼 격자지도를 중복으로 검사하는 것이 효율성에 많은 영향을 미친 것 같더라고요
새로운 방법은 격자지도의 모든 점을 확인하는 방법입니다.
아래 그림과 같이 오른쪽, 왼쪽, 위쪽, 아래쪽으로 한번씩 훑으면서 격자지도의 좌표가 guard들에 의해 탐색되었는지 확인합니다.

그런데... runtime 효율성이 생각보다 많이 개선되지는 않았습니다.
다음에는 leetcode의 솔루션과 다른 풀이가 있는지 찾아봐야겠네요!

+ 코드
더보기
class Solution:
def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:
grid_map = [[0] * n for _ in range(m)]
UNGUARDED = 0
GUARDED = 1
GUARD = 2
WALL = 3
for wall in walls:
grid_map[wall[0]][wall[1]] = WALL
for guard in guards:
grid_map[guard[0]][guard[1]] = GUARD
# check right direction
for row_idx in range(m):
is_guarded = UNGUARDED
for col_idx in range(n):
if grid_map[row_idx][col_idx] == GUARD:
is_guarded = GUARDED
elif grid_map[row_idx][col_idx] == WALL:
is_guarded = UNGUARDED
elif is_guarded == GUARDED:
grid_map[row_idx][col_idx] = GUARDED
# check left direction
for row_idx in range(m):
is_guarded = UNGUARDED
for col_idx in range(n)[::-1]:
if grid_map[row_idx][col_idx] == GUARD:
is_guarded = GUARDED
elif grid_map[row_idx][col_idx] == WALL:
is_guarded = UNGUARDED
elif is_guarded == GUARDED:
grid_map[row_idx][col_idx] = GUARDED
# check up direction
for col_idx in range(n):
is_guarded = UNGUARDED
for row_idx in range(m)[::-1]:
if grid_map[row_idx][col_idx] == GUARD:
is_guarded = GUARDED
elif grid_map[row_idx][col_idx] == WALL:
is_guarded = UNGUARDED
elif is_guarded == GUARDED:
grid_map[row_idx][col_idx] = GUARDED
# check down direction
for col_idx in range(n):
is_guarded = UNGUARDED
for row_idx in range(m):
if grid_map[row_idx][col_idx] == GUARD:
is_guarded = GUARDED
elif grid_map[row_idx][col_idx] == WALL:
is_guarded = UNGUARDED
elif is_guarded == GUARDED:
grid_map[row_idx][col_idx] = GUARDED
result = 0
for row_idx in range(m):
for col_idx in range(n):
if grid_map[row_idx][col_idx] == UNGUARDED:
result += 1
return result
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
| [leetcode] 75. Sort Colors - 이번에는 Runtime 100% 찍었습니다! (0) | 2025.05.20 |
|---|---|
| [leetcode] 2257. Count Unguarded Cells in the Grid #3 (2) | 2024.11.29 |
| [leetcode] 2257. Count Unguarded Cells in the Grid (1) | 2024.11.23 |
| [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 |