From 4275c20994fa36f71b2a5c0348affd16baad4d0b Mon Sep 17 00:00:00 2001 From: Gyunseo Lee Date: Fri, 7 Jun 2024 16:24:34 +0900 Subject: [PATCH] 2024-06-07 16:24:34 Affected files: .obsidian/workspace.json src/content/blog/leet-code-minimum-path-sum.md --- .obsidian/workspace.json | 35 ++++++------ .../blog/leet-code-minimum-path-sum.md | 54 +++++++++++++++++++ 2 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 src/content/blog/leet-code-minimum-path-sum.md diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 04eb870f9..2ec72a64a 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -4,35 +4,40 @@ "type": "split", "children": [ { - "id": "4d80a0b9491269e4", + "id": "af13f5421c20f0f4", "type": "tabs", "children": [ { - "id": "0f0fd716299d38e2", + "id": "6744425213e811d2", "type": "leaf", "state": { "type": "markdown", "state": { - "file": "src/content/blog/boj-5014-스타트링크.md", + "file": "src/content/blog/boj-1940-주몽.md", "mode": "source", "source": false } } - }, + } + ] + }, + { + "id": "1e447cff571d4449", + "type": "tabs", + "children": [ { - "id": "6352edd452388512", + "id": "7b370b6893c04ec7", "type": "leaf", "state": { "type": "markdown", "state": { - "file": "src/content/blog/boj-random-defense.md", + "file": "src/content/blog/leet-code-minimum-path-sum.md", "mode": "source", "source": false } } } - ], - "currentTab": 1 + ] } ], "direction": "vertical" @@ -99,7 +104,7 @@ "state": { "type": "backlink", "state": { - "file": "src/content/blog/boj-random-defense.md", + "file": "src/content/blog/leet-code-minimum-path-sum.md", "collapseAll": false, "extraContext": false, "sortOrder": "alphabetical", @@ -116,7 +121,7 @@ "state": { "type": "outgoing-link", "state": { - "file": "src/content/blog/boj-random-defense.md", + "file": "src/content/blog/leet-code-minimum-path-sum.md", "linksCollapsed": false, "unlinkedCollapsed": true } @@ -139,7 +144,7 @@ "state": { "type": "outline", "state": { - "file": "src/content/blog/boj-random-defense.md" + "file": "src/content/blog/leet-code-minimum-path-sum.md" } } } @@ -162,8 +167,10 @@ "table-editor-obsidian:Advanced Tables Toolbar": false } }, - "active": "6352edd452388512", + "active": "7b370b6893c04ec7", "lastOpenFiles": [ + "src/content/blog/boj-1940-주몽.md", + "src/content/blog/leet-code-minimum-path-sum.md", "src/content/blog/boj-5014-스타트링크.md", "src/content/blog/boj-random-defense.md", "src/content/blog/boj-14425-문자열-집합.md", @@ -196,7 +203,6 @@ "src/content/blog/boj-11507-오르막-수.md", "src/content/blog/boj-10819-차이를-최대로.md", "src/content/blog/boj-2230-수-고르기.md", - "src/content/blog/boj-1940-주몽.md", "src/content/blog/boj-1912-연속합.md", "src/content/blog/boj-1699-제곱수의-합.md", "src/content/blog/boj-1026-보물.md", @@ -208,7 +214,6 @@ "Excalidraw/Drawing 2024-03-24 18.59.55.excalidraw.md", "src/content/blog/change-apt-get-source-server.md", "src/content/blog/personal-statement-for-an-application.md", - "src/content/blog/software-maestro-application.md", - "src/content/blog/24-sw-maestro-1st-coding-test.md" + "src/content/blog/software-maestro-application.md" ] } \ No newline at end of file diff --git a/src/content/blog/leet-code-minimum-path-sum.md b/src/content/blog/leet-code-minimum-path-sum.md new file mode 100644 index 000000000..f8d69672b --- /dev/null +++ b/src/content/blog/leet-code-minimum-path-sum.md @@ -0,0 +1,54 @@ +--- +author: Gyunseo Lee +title: "LeetCode: Minimum Path Sum" +pubDatetime: 2024-06-07T16:20:00+09:00 +modDatetime: 2024-06-07T16:20:00+09:00 +featured: false +draft: false +tags: + - DP + - PS + - Algorithms + - LeetCode +description: DP 웰노운인 최소 거리 합 문제를 풀어 보자... +ogImage: "" +--- + +## Table of contents + +## 들어가며 + +이 문제는 움직일 수 있는 방향이 오른쪽과 아래 방향이어서 쉽게 DP 문제로 생각해 볼 수 있었습니다. 👍 + +## AC 받은 Python 코드 + +```python +from typing import List +import sys + +input = sys.stdin.readline + + +class Solution: + def minPathSum(self, grid: List[List[int]]) -> int: + len_row = len(grid) + len_col = len(grid[0]) + for i in range(len_row): + for j in range(len_col): + if i == 0 and j == 0: + continue + + if i == 0: + grid[i][j] = grid[i][j - 1] + grid[i][j] + elif j == 0: + grid[i][j] = grid[i - 1][j] + grid[i][j] + else: + grid[i][j] = min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j] + return grid[len_row - 1][len_col - 1] + + +if __name__ == "__main__": + sol = Solution() + grid = eval(input().rstrip()) + print(sol.minPathSum(grid)) +```