forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEgonD3V.py
More file actions
38 lines (29 loc) ยท 1.08 KB
/
EgonD3V.py
File metadata and controls
38 lines (29 loc) ยท 1.08 KB
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
32
33
34
35
36
37
38
from unittest import TestCase, main
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
return self.solve_dp(m, n)
"""
Runtime: 33 ms (Beats 73.78%)
Time Complexity: O(m * n)
- m * n ํฌ๊ธฐ์ 2์ฐจ์ ๋ฐฐ์ด dp ์์ฑ์ O(m * n)
- range(1, m) * range(1, n) ์ค์ฒฉ ์กฐํ์ O((m - 1) * (n - 1))
- result๋ก dp[-1][-1] ์กฐํ์ O(1)
> O(m * n) + O((m - 1) * (n - 1)) + O(1) ~= O(m * n) + O(m * n) ~= O(m * n)
Memory: 16.44 (Beats 77.15%)
Space Complexity: O(m * n)
> m * n ํฌ๊ธฐ์ 2์ฐจ์ ๋ฐฐ์ด dp ์ฌ์ฉ์ O(m * n)
"""
def solve_dp(self, m: int, n: int) -> int:
dp = [[1 if r == 0 or c == 0 else 0 for c in range(n)] for r in range(m)]
for r in range(1, m):
for c in range(1, n):
dp[r][c] = dp[r - 1][c] + dp[r][c - 1]
return dp[-1][-1]
class _LeetCodeTestCases(TestCase):
def test_1(self):
m = 3
n = 7
output = 28
self.assertEqual(Solution.uniquePaths(Solution(), m, n), output)
if __name__ == '__main__':
main()