Notice
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
- 그래프
- 분산처리
- 분산
- 아파치 하둡
- programmers
- leetcode
- Apache Spark
- 하둡
- 티스토리챌린지
- 알고리즘
- 우선순위큐
- 코딩테스트
- 도커
- docker
- 아파치 카프카
- 카프카
- String
- 문자열
- heapq
- BFS
- DP
- 아파치 스파크
- 리트코드
- 파이썬
- 오블완
- 프로그래머스
- Python
- Apache Hadoop
- apache kafka
- 이진탐색
Archives
- Today
- Total
래원
[LeetCode] 2658. Maximum Number of Fish in a Grid (Python) 본문
알고리즘/LeetCode
[LeetCode] 2658. Maximum Number of Fish in a Grid (Python)
Laewon Jeong 2025. 1. 28. 19:43
난이도: Medium
문제 설명
You are given a 0-indexed 2D matrix grid
of size m x n
, where (r, c)
represents:
- A land cell if
grid[r][c] = 0
, or - A water cell containing
grid[r][c]
fish, ifgrid[r][c] > 0
.
A fisher can start at any water cell (r, c)
and can do the following operations any number of times:
- Catch all the fish at cell
(r, c)
, or - Move to any adjacent water cell.
Return the maximum number of fish the fisher can catch if he chooses his starting cell optimally, or 0
if no water cell exists.
An adjacent cell of the cell (r, c)
, is one of the cells (r, c + 1)
, (r, c - 1)
, (r + 1, c)
or (r - 1, c)
if it exists.
문제 예제
Example 1:
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
Output: 7
Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.
Example 2:
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
Output: 1
Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish.
제한 사항
m == grid.length
n == grid[i].length
1 <= m, n <= 10
0 <= grid[i][j] <= 10
Solution(솔루션)
class Solution:
def findMaxFish(self, grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])
moves = [[0,1], [0,-1], [1,0], [-1,0]]
visited = [[False] * m for _ in range(n)]
answer = 0
for i in range(n):
for j in range(m):
if grid[i][j] != 0:
q = deque([])
q.append((i,j))
visited[i][j] = True
fish = 0
while q:
x, y = q.popleft()
fish += grid[x][y]
for move in moves:
nx = x + move[0]
ny = y + move[1]
if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and grid[nx][ny] != 0:
visited[nx][ny] = True
q.append((nx, ny))
answer = max(answer, fish)
return answer
bfs를 통해 문제를 해결했지만, dfs로도 충분히 해결할 수 있을 것 같다.
모든 0이 아닌 cell에서 시작하여 상하좌우 움직이며 모을 수 있는 fish의 수를 세었다.
센 fish의 수를 answer와 비교해 answer보다 크면 이를 answer에 저장했다.
마지막에는 이 answer를 return하여 정답을 맞을 수 있었다.
문제: 2658. Maximum Number of Fish in a Grid
깃허브: github
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode] 785. Is Graph Bipartite? (Python) (0) | 2025.01.30 |
---|---|
[LeetCode] 1462. Course Schedule IV (Python) (0) | 2025.01.27 |
[LeetCode] 2948. Make Lexicographically Smallest Array by Swapping Elements (Python) (0) | 2025.01.25 |
[LeetCode] 802. Find Eventual Safe States (Python) (0) | 2025.01.24 |
[LeetCode] 1267. Count Servers that Communicate (Python) (0) | 2025.01.23 |