반응형
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
- 회고
- 논문 정리
- 오블완
- 경로 계획 알고리즘
- study
- 14일 공부
- 커머스
- AWS 비용 절감
- 청첩장 모임
- 지표
- 디자인 패턴
- PostgreSQL
- terraform
- Go-lang
- leetcode
- 생성 패턴
- github
- docker
- Playwright
- 티스토리챌린지
- AWS
- amazon ecs
- Rust
- 신혼 여행
- ssh
- Til
- 구조 패턴
- 실용주의 프로그래머
- DevOps
- MAPF
Archives
- Today
- Total
밤 늦게까지 여는 카페
[leetcode] 2257. Count Unguarded Cells in the Grid #3 본문
반응형
이전에 풀었던 2257. Count Unguarded Cells in the Grid 문제를 3번째로 도전하는 중입니다.
이번에는 다른 분들이 푼 솔루션을 보고 개선 방법을 찾아봤습니다.
아뿔싸...
첫번째로 시도한 방법의 구현만 바꿨을뿐인데 100%인 답이 있더라고요...
첫번째 풀이에서 walls_dict, visited를 관리하는 대신에 m x n 짜리 격자지도를 표현할 수 있는 이중 리스트를 만드는 편이 훨씬 빠르더라고요...!
- dictionary와 list의 성능 차이인 것 같은데 이것은 나중에 확인해봐야겠네요

+코드
더보기
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)]
for wall in walls:
grid_map[wall[0]][wall[1]] = 2
for guard in guards:
grid_map[guard[0]][guard[1]] = 2
for guard in guards:
[row_of_guard, col_of_guard] = guard
for row in range(row_of_guard-1, -1, -1):
if grid_map[row][col_of_guard] == 2:
break
grid_map[row][col_of_guard] = 1
for row in range(row_of_guard+1, len(grid_map)):
if grid_map[row][col_of_guard] == 2:
break
grid_map[row][col_of_guard] = 1
for col in range(col_of_guard-1, -1, -1):
if grid_map[row_of_guard][col] == 2:
break
grid_map[row_of_guard][col] = 1
for col in range(col_of_guard+1, len(grid_map[0])):
if grid_map[row_of_guard][col] == 2:
break
grid_map[row_of_guard][col] = 1
return sum(row.count(0) for row in grid_map)
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
| [leetcode] 399. Evaluate Division - 이번에도 Runtime 100%! (0) | 2025.06.03 |
|---|---|
| [leetcode] 75. Sort Colors - 이번에는 Runtime 100% 찍었습니다! (0) | 2025.05.20 |
| [leetcode] 2257. Count Unguarded Cells in the Grid #2 (1) | 2024.11.27 |
| [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 |