Skip to content

Commit 0c8f2ed

Browse files
committed
Adding solution of 64, 72, 85 problems
1 parent 941d73d commit 0c8f2ed

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

64.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
3+
4+
Note: You can only move either down or right at any point in time.
5+
'''
6+
class Solution(object):
7+
def minPathSum(self, grid):
8+
"""
9+
:type grid: List[List[int]]
10+
:rtype: int
11+
"""
12+
if not grid:
13+
return 0
14+
15+
row, col = len(grid), len(grid[0])
16+
dp = [[0 for _ in range(col)] for _ in range(row)]
17+
dp[0][0] = grid[0][0]
18+
19+
for index in range(1, row):
20+
dp[index][0] = dp[index-1][0] + grid[index][0]
21+
22+
for index in range(1, col):
23+
dp[0][index] = dp[0][index-1] + grid[0][index]
24+
25+
print dp
26+
for index_i in range(1, row):
27+
for index_j in range(1, col):
28+
dp[index_i][index_j] = min(dp[index_i-1][index_j], dp[index_i][index_j-1]) + grid[index_i][index_j]
29+
30+
return dp[row-1][col-1]

72.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
3+
4+
You have the following 3 operations permitted on a word:
5+
6+
Insert a character
7+
Delete a character
8+
Replace a character
9+
Example 1:
10+
11+
Input: word1 = "horse", word2 = "ros"
12+
Output: 3
13+
Explanation:
14+
horse -> rorse (replace 'h' with 'r')
15+
rorse -> rose (remove 'r')
16+
rose -> ros (remove 'e')
17+
Example 2:
18+
19+
Input: word1 = "intention", word2 = "execution"
20+
Output: 5
21+
Explanation:
22+
intention -> inention (remove 't')
23+
inention -> enention (replace 'i' with 'e')
24+
enention -> exention (replace 'n' with 'x')
25+
exention -> exection (replace 'n' with 'c')
26+
exection -> execution (insert 'u')
27+
'''
28+
29+
class Solution(object):
30+
def minDistance(self, word1, word2):
31+
"""
32+
:type word1: str
33+
:type word2: str
34+
:rtype: int
35+
"""
36+
m , n = len(word1), len(word2)
37+
38+
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
39+
for index_i in range(m+1):
40+
for index_j in range(n+1):
41+
if index_i == 0:
42+
dp[index_i][index_j] = index_j
43+
elif index_j == 0:
44+
dp[index_i][index_j] = index_i
45+
elif word1[index_i-1] == word2[index_j-1]:
46+
dp[index_i][index_j] = dp[index_i-1][index_j-1]
47+
else:
48+
dp[index_i][index_j] = 1 + min(dp[index_i-1][index_j], dp[index_i-1][index_j-1], dp[index_i][index_j-1])
49+
50+
return dp[m][n]

85.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
3+
4+
Example:
5+
6+
Input:
7+
[
8+
["1","0","1","0","0"],
9+
["1","0","1","1","1"],
10+
["1","1","1","1","1"],
11+
["1","0","0","1","0"]
12+
]
13+
Output: 6
14+
'''
15+
16+
class Solution(object):
17+
def largestRectangleArea(self, heights):
18+
"""
19+
:type heights: List[int]
20+
:rtype: int
21+
"""
22+
if not heights:
23+
return 0
24+
25+
stack = []
26+
result, index = 0, 0
27+
28+
while index < len(heights):
29+
if not stack or heights[index] >= heights[stack[-1]]:
30+
stack.append(index)
31+
index += 1
32+
else:
33+
curr = stack.pop()
34+
if not stack:
35+
area = heights[curr]*index
36+
else:
37+
area = heights[curr] * (index-stack[-1]-1)
38+
result = max(result, area)
39+
40+
while stack:
41+
curr = stack.pop()
42+
if not stack:
43+
area = heights[curr]*index
44+
else:
45+
area = heights[curr] * (index-stack[-1]-1)
46+
result = max(result, area)
47+
return result
48+
49+
def maximalRectangle(self, matrix):
50+
"""
51+
:type matrix: List[List[str]]
52+
:rtype: int
53+
"""
54+
if not matrix:
55+
return 0
56+
m, n = len(matrix), len(matrix[0])
57+
heights = [0 for index in range(n)]
58+
result = 0
59+
60+
for index_i in range(m):
61+
for index_j in range(n):
62+
if matrix[index_i][index_j] != '0':
63+
heights[index_j] = heights[index_j] + 1
64+
else:
65+
heights[index_j] = 0
66+
67+
result = max(result, self.largestRectangleArea(heights))
68+
return result
69+

0 commit comments

Comments
 (0)