Skip to content

Commit f93126a

Browse files
committed
feat(python): solutions
1 parent cc7af24 commit f93126a

File tree

7 files changed

+315
-39
lines changed

7 files changed

+315
-39
lines changed

JavaScript/0062. Unique Paths.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,13 @@
2929

3030
// 1) Dynamic programming
3131
// Time O(mn)
32-
// Space O(n)
32+
// Space O(mn)
3333
//
3434
// Example
35-
// 1 1 (1) 1 1 1 (init current row to 1)
35+
// 1 1 (1) 1 1 1
3636
// 1 (2) (3) 4 5 6 (e.g. 3 = 1 + 2)
37-
// 1 3 6 10 15 21 (return the last number in the last row which is 21)
37+
// 1 3 6 10 15 21
3838
const uniquePaths1 = (m, n) => {
39-
const row = Array(n).fill(1);
40-
for (let i = 1; i < m; i++) {
41-
for (let j = 1; j < n; j++) {
42-
row[j] += row[j - 1];
43-
}
44-
}
45-
return row[n - 1];
46-
};
47-
48-
// 2)
49-
// Time O(mn)
50-
// Space O(mn)
51-
const uniquePaths = (m, n) => {
5239
// init first row and col to 1, and the rest to 0
5340
const matrix = [];
5441
for (let i = 0; i < m; i++) {
@@ -67,3 +54,28 @@ const uniquePaths = (m, n) => {
6754
}
6855
return matrix[m - 1][n - 1];
6956
};
57+
58+
// 2) Dynamic programming (optimization)
59+
// Time O(mn)
60+
// Space O(n)
61+
//
62+
// i j dp
63+
// 1 1 [1, 2, 1, 1, 1, 1]
64+
// 1 2 [1, 2, 3, 1, 1, 1]
65+
// 1 3 [1, 2, 3, 4, 1, 1]
66+
// 1 4 [1, 2, 3, 4, 5, 1]
67+
// 1 5 [1, 2, 3, 4, 5, 6]
68+
// 2 1 [1, 3, 3, 4, 5, 6]
69+
// 2 2 [1, 3, 6, 4, 5, 6]
70+
// 2 3 [1, 3, 6, 10, 5, 6]
71+
// 2 4 [1, 3, 6, 10, 15, 6]
72+
// 2 5 [1, 3, 6, 10, 15, 21]
73+
const uniquePaths = (m, n) => {
74+
const row = Array(n).fill(1);
75+
for (let i = 1; i < m; i++) {
76+
for (let j = 1; j < n; j++) {
77+
row[j] += row[j - 1];
78+
}
79+
}
80+
return row[n - 1];
81+
};

JavaScript/0066. Plus One.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ const plusOne = (digits) => {
2424
let i = digits.length;
2525

2626
while (i--) {
27-
digits[i] += 1;
28-
if (digits[i] < 10) return digits;
27+
if (digits[i] < 9) {
28+
digits[i] += 1;
29+
return digits;
30+
}
2931
digits[i] = 0;
3032
}
3133

JavaScript/0070. Climbing Stairs.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ const climbStairs1 = (n) => {
3939
// Space O(n)
4040
const climbStairs2 = (n) => {
4141
const map = {};
42-
const go = (n) => {
43-
if (n < 2) return 1;
44-
if (map[n] != null) return map[n];
45-
map[n] = go(n - 2) + go(n - 1);
46-
return map[n];
42+
const go = (i) => {
43+
if (i < 2) return 1;
44+
if (map[i] != null) return map[i];
45+
map[i] = go(i - 2) + go(i - 1);
46+
return map[i];
4747
};
4848
return go(n);
4949
};

Python/0062. Unique Paths.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
2+
# Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
3+
# The test cases are generated so that the answer will be less than or equal to 2 * 10^9.
4+
#
5+
# Example 1:
6+
#
7+
# Input: m = 3, n = 7
8+
# Output: 28
9+
#
10+
# Example 2:
11+
#
12+
# Input: m = 3, n = 2
13+
# Output: 3
14+
# Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
15+
# 1. Right -> Down -> Down
16+
# 2. Down -> Down -> Right
17+
# 3. Down -> Right -> Down
18+
#
19+
# Constraints:
20+
#
21+
# 1 <= m, n <= 100
22+
23+
24+
# 1) Dynamic programming
25+
# Time O(mn)
26+
# Space O(mn)
27+
#
28+
# Example
29+
# 1 1 (1) 1 1 1
30+
# 1 (2) (3) 4 5 6 (e.g. 3 = 1 + 2)
31+
# 1 3 6 10 15 21
32+
class Solution:
33+
def uniquePaths(self, m: int, n: int) -> int:
34+
dp = [[0] * n for _ in range(m)]
35+
for i in range(m):
36+
for j in range(n):
37+
if i == 0 or j == 0:
38+
dp[i][j] = 1
39+
else:
40+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
41+
return dp[-1][-1]
42+
43+
44+
# 2) Dynamic programming (optimization)
45+
# Time O(mn)
46+
# Space O(n)
47+
#
48+
# Example
49+
# i j dp
50+
# 1 1 [1, 2, 1, 1, 1, 1]
51+
# 1 2 [1, 2, 3, 1, 1, 1]
52+
# 1 3 [1, 2, 3, 4, 1, 1]
53+
# 1 4 [1, 2, 3, 4, 5, 1]
54+
# 1 5 [1, 2, 3, 4, 5, 6]
55+
# 2 1 [1, 3, 3, 4, 5, 6]
56+
# 2 2 [1, 3, 6, 4, 5, 6]
57+
# 2 3 [1, 3, 6, 10, 5, 6]
58+
# 2 4 [1, 3, 6, 10, 15, 6]
59+
# 2 5 [1, 3, 6, 10, 15, 21]
60+
class Solution:
61+
def uniquePaths(self, m: int, n: int) -> int:
62+
dp = [1] * n
63+
for _ in range(1, m):
64+
for j in range(1, n):
65+
dp[j] += dp[j - 1]
66+
return dp[-1]

Python/0066. Plus One.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
# Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
2-
#
3-
# The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
4-
#
5-
# You may assume the integer does not contain any leading zero, except the number 0 itself.
1+
# You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
2+
# Increment the large integer by one and return the resulting array of digits.
63
#
74
# Example 1:
85
#
9-
# Input: [1,2,3]
6+
# Input: digits = [1,2,3]
107
# Output: [1,2,4]
118
# Explanation: The array represents the integer 123.
9+
# Incrementing by one gives 123 + 1 = 124.
10+
# Thus, the result should be [1,2,4].
1211
#
1312
# Example 2:
1413
#
15-
# Input: [4,3,2,1]
14+
# Input: digits = [4,3,2,1]
1615
# Output: [4,3,2,2]
1716
# Explanation: The array represents the integer 4321.
17+
# Incrementing by one gives 4321 + 1 = 4322.
18+
# Thus, the result should be [4,3,2,2].
19+
#
20+
# Example 3:
21+
#
22+
# Input: digits = [9]
23+
# Output: [1,0]
24+
# Explanation: The array represents the integer 9.
25+
# Incrementing by one gives 9 + 1 = 10.
26+
# Thus, the result should be [1,0].
27+
#
28+
# Constraints:
29+
#
30+
# 1 <= digits.length <= 100
31+
# 0 <= digits[i] <= 9
32+
# digits does not contain any leading 0's.
1833

1934

2035
class Solution:
2136
def plusOne(self, digits: List[int]) -> List[int]:
22-
i = len(digits) - 1
23-
24-
while i >= 0:
25-
digits[i] += 1
26-
27-
if digits[i] < 10:
37+
for i in range(len(digits) - 1, -1, -1):
38+
if digits[i] < 9:
39+
digits[i] += 1
2840
return digits
29-
3041
digits[i] = 0
31-
i -= 1
32-
3342
digits.insert(0, 1)
3443
return digits

Python/0070. Climbing Stairs.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# You are climbing a staircase. It takes n steps to reach the top.
2+
# Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
3+
#
4+
# Example 1:
5+
#
6+
# Input: n = 2
7+
# Output: 2
8+
# Explanation: There are two ways to climb to the top.
9+
# 1. 1 step + 1 step
10+
# 2. 2 steps
11+
#
12+
# Example 2:
13+
#
14+
# Input: n = 3
15+
# Output: 3
16+
# Explanation: There are three ways to climb to the top.
17+
# 1. 1 step + 1 step + 1 step
18+
# 2. 1 step + 2 steps
19+
# 3. 2 steps + 1 step
20+
#
21+
# Constraints:
22+
#
23+
# 1 <= n <= 45
24+
25+
26+
# 1) Recursion (time limit exceeded)
27+
# Time O(2^n) - O(branch ^ recursion depth)
28+
# Space O(n) - O(recursion depth)
29+
class Solution:
30+
def climbStairs(self, n: int) -> int:
31+
if n == 1:
32+
return 1
33+
if n == 2:
34+
return 2
35+
return self.climbStairs(n - 2) + self.climbStairs(n - 1)
36+
37+
38+
# 2) Recursion (memoization)
39+
# Time O(n)
40+
# Space O(n)
41+
class Solution:
42+
def climbStairs(self, n: int) -> int:
43+
dic = [None] * (n + 1)
44+
45+
def go(i):
46+
if i == 1:
47+
return 1
48+
if i == 2:
49+
return 2
50+
if dic[i] is not None:
51+
return dic[i]
52+
dic[i] = go(i - 2) + go(i - 1)
53+
return dic[i]
54+
55+
return go(n)
56+
57+
58+
# 3) Dynamic programming - Fibonacci
59+
# Similar
60+
# 70. Climbing Stairs
61+
# 91. Decode Ways
62+
#
63+
# Time O(n)
64+
# Space O(n)
65+
class Solution:
66+
def climbStairs(self, n: int) -> int:
67+
if n == 1:
68+
return 1
69+
if n == 2:
70+
return 2
71+
dp = [None] * (n + 1)
72+
dp[1] = 1
73+
dp[2] = 2
74+
for i in range(3, n + 1):
75+
dp[i] = dp[i - 2] + dp[i - 1]
76+
return dp[-1]
77+
78+
79+
# 4) Dynamic programming - Fibonacci (optimization)
80+
# Time O(n)
81+
# Space O(1)
82+
class Solution:
83+
def climbStairs(self, n: int) -> int:
84+
if n == 1:
85+
return 1
86+
if n == 2:
87+
return 2
88+
a = 1
89+
b = 2
90+
for i in range(3, n + 1):
91+
c = a + b
92+
a = b
93+
b = c
94+
return c

0 commit comments

Comments
 (0)