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 |
Tags
- 코딩테스트 준비
- Python
- infcon 2024
- 단기개발자코스
- 커스텀 헤더
- 파이썬
- 인프콘 2024
- JavaScript
- 프로그래머스 이중우선순위큐
- spring batch 5.0
- 디자인 패턴
- 빈 조회 2개 이상
- DesignPattern
- 99클럽
- KPT회고
- @FeignClient
- 항해99
- 1주일회고
- 프로그래머스
- 개발자부트캠프추천
- 개발자 취업
- 디자인패턴
- TiL
- 취업리부트코스
- 전략패턴 #StrategyPattern #디자인패턴
- Spring multimodule
- jwt
- jwttoken
- 빈 충돌
- 구글 OAuth login
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 64. Minimum Path Sum with Python 본문
https://leetcode.com/problems/minimum-path-sum/description/
bfs로 접근했다가 시간초과가 났던 문제
생각해보니까 그럼 앞서 갔던 길들을 최적으로 다시 다 바꿔줘야해서 bfs를 사용하면 안된다는 생각이 들었다.
그러다 생각이 든게 어짜피 오른쪽, 아래쪽으로밖에 움직이지 않으니까
위 한줄, 왼쪽 한줄의 비용을 미리 계산해놓고 위에서 오는게 cost가 적은지 왼쪽에서 오는게 cost가 적은지 판단하면 된다고 생각했다.
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
# 그래프의 가로 길이
m = len(grid[0])
# 그래프의 세로 길아
n = len(grid)
costs = [[0 for _ in range(m)] for _ in range(n)]
costs[0][0] = grid[0][0]
# 위 1줄, 왼쪽 1줄 cost 계산
for i in range(n):
for j in range(m):
if i == 0 and j > 0:
costs[i][j] = grid[i][j] + costs[i][j-1]
elif j == 0 and i > 0:
costs[i][j] = grid[i][j] + costs[i-1][j]
for i in range(1, n):
for j in range(1, m):
costs[i][j] = min(grid[i][j] + costs[i-1][j], grid[i][j] + costs[i][j-1])
return costs[n-1][m-1]
'알고리즘 with python > 20240909' 카테고리의 다른 글
프로그래머스 입국심사 with Python (0) | 2024.10.10 |
---|---|
프로그래머스 최소직사각형 with Python (1) | 2024.09.13 |
프로그래머스 K번째수 with Python (0) | 2024.09.12 |
프로그래머스 같은 숫자는 싫어 with Python (0) | 2024.09.11 |
프로그래머스 완주하지 못한 선수 with Python (0) | 2024.09.10 |