Skip to content

Commit 768050c

Browse files
committed
Fixed solutions
1 parent 7c7010c commit 768050c

File tree

9 files changed

+237
-0
lines changed

9 files changed

+237
-0
lines changed

minimum_path_sum/solution.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
Given a m x n grid filled with non-negative numbers, find a path from top left
3+
to bottom right which minimizes the sum of all numbers along its path.
4+
5+
Note: You can only move either down or right at any point in time.
6+
"""
7+
18
class Solution:
29
# @param grid, a list of lists of integers
310
# @return an integer

minimum_path_sum/solution2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
Given a m x n grid filled with non-negative numbers, find a path from top left
3+
to bottom right which minimizes the sum of all numbers along its path.
4+
5+
Note: You can only move either down or right at any point in time.
6+
"""
7+
18
class Solution:
29
# @param grid, a list of lists of integers
310
# @return an integer

search_for_a_range/solution2.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Given a sorted array of integers, find the starting and ending position of a
3+
given target value.
4+
5+
Your algorithm's runtime complexity must be in the order of O(log n).
6+
7+
If the target is not found in the array, return [-1, -1].
8+
9+
For example,
10+
Given [5, 7, 7, 8, 8, 10] and target value 8,
11+
return [3, 4].
12+
"""
13+
14+
class Solution(object):
15+
def searchRange(self, nums, target):
16+
"""
17+
:type nums: List[int]
18+
:type target: int
19+
:rtype: List[int]
20+
"""
21+
n = len(nums)
22+
left = 0
23+
right = n - 1
24+
left_res = -1
25+
right_res = -1
26+
# Search for left bound (first occurence of target)
27+
while left + 1 < right:
28+
mid = left + (right - left) / 2
29+
if target > nums[mid]:
30+
left = mid
31+
else:
32+
right = mid
33+
if nums[left] == target:
34+
left_res = left
35+
elif nums[right] == target:
36+
left_res = right
37+
else:
38+
return [-1, -1]
39+
40+
# Search for right bound (last occurence of target)
41+
left = 0
42+
right = n - 1
43+
while left + 1 < right:
44+
mid = left + (right - left) / 2
45+
if target >= nums[mid]:
46+
left = mid
47+
else:
48+
right = mid
49+
if nums[right] == target:
50+
right_res = right
51+
elif nums[left] == target:
52+
right_res = left
53+
return [left_res, right_res]
54+
55+
56+
a1 = [5, 7, 7, 8, 8, 10]
57+
a2 = [2, 2]
58+
s = Solution()
59+
print(s.searchRange(a1, 8))
60+
print(s.searchRange(a2, 2))

triangle/solution.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
"""
2+
Given a triangle, find the minimum path sum from top to bottom. Each step you
3+
may move to adjacent numbers on the row below.
4+
5+
For example, given the following triangle
6+
[
7+
[2],
8+
[3,4],
9+
[6,5,7],
10+
[4,1,8,3]
11+
]
12+
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
13+
14+
Note:
15+
Bonus point if you are able to do this using only O(n) extra space, where n is
16+
the total number of rows in the triangle.
17+
"""
18+
119
class Solution:
220
# @param triangle, a list of lists of integers
321
# @return an integer

triangle/solution2.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Given a triangle, find the minimum path sum from top to bottom. Each step you
3+
may move to adjacent numbers on the row below.
4+
5+
For example, given the following triangle
6+
[
7+
[2],
8+
[3,4],
9+
[6,5,7],
10+
[4,1,8,3]
11+
]
12+
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
13+
14+
Note:
15+
Bonus point if you are able to do this using only O(n) extra space, where n is
16+
the total number of rows in the triangle.
17+
"""
18+
19+
class Solution(object):
20+
def minimumTotal(self, triangle):
21+
"""
22+
:type triangle: List[List[int]]
23+
:rtype: int
24+
"""
25+
n = len(triangle)
26+
m = 0
27+
res = None
28+
for i, row in enumerate(triangle):
29+
m = len(row)
30+
if i > 0:
31+
for j, col in enumerate(row):
32+
if 0 < j < m - 1:
33+
triangle[i][j] += min(triangle[i - 1][j - 1],
34+
triangle[i - 1][j])
35+
elif j == 0:
36+
triangle[i][j] += triangle[i - 1][j]
37+
# j == m - 1
38+
else:
39+
triangle[i][j] += triangle[i - 1][j - 1]
40+
if i == n - 1:
41+
for col in row:
42+
if res is None:
43+
res = col
44+
else:
45+
res = min(col, res)
46+
return res
47+
48+
49+
a1 = [
50+
[2],
51+
[3, 4],
52+
[6, 5, 7],
53+
[4, 1, 8, 3]
54+
]
55+
56+
s = Solution()
57+
print(s.minimumTotal(a1))

unique_paths/solution.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
"""
2+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in
3+
the diagram below).
4+
5+
The robot can only move either down or right at any point in time. The robot
6+
is trying to reach the bottom-right corner of the grid (marked 'Finish' in the
7+
diagram below).
8+
9+
How many possible unique paths are there?
10+
"""
11+
112
class Solution:
213
# @return an integer
314
def uniquePaths(self, m, n):

unique_paths_ii/solution.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
"""
2+
Follow up for "Unique Paths":
3+
4+
Now consider if some obstacles are added to the grids. How many unique paths
5+
would there be?
6+
7+
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
8+
9+
For example,
10+
There is one obstacle in the middle of a 3x3 grid as illustrated below.
11+
12+
[
13+
[0,0,0],
14+
[0,1,0],
15+
[0,0,0]
16+
]
17+
The total number of unique paths is 2.
18+
19+
Note: m and n will be at most 100.
20+
"""
21+
122
class Solution:
223
# @param obstacleGrid, a list of lists of integers
324
# @return an integer

unique_paths_ii/solution2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
"""
2+
Follow up for "Unique Paths":
3+
4+
Now consider if some obstacles are added to the grids. How many unique paths
5+
would there be?
6+
7+
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
8+
9+
For example,
10+
There is one obstacle in the middle of a 3x3 grid as illustrated below.
11+
12+
[
13+
[0,0,0],
14+
[0,1,0],
15+
[0,0,0]
16+
]
17+
The total number of unique paths is 2.
18+
19+
Note: m and n will be at most 100.
20+
"""
21+
122
class Solution:
223
# @param obstacleGrid, a list of lists of integers
324
# @return an integer

word_break/solution.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Given a string s and a dictionary of words dict, determine if s can be
3+
segmented into a space-separated sequence of one or more dictionary words.
4+
5+
For example, given
6+
s = "leetcode",
7+
dict = ["leet", "code"].
8+
9+
Return true because "leetcode" can be segmented as "leet code".
10+
"""
11+
12+
class Solution(object):
13+
def wordBreak(self, s, wordDict):
14+
"""
15+
:type s: str
16+
:type wordDict: Set[str]
17+
:rtype: bool
18+
"""
19+
t = set()
20+
return self.word_break_aux(s, wordDict, t)
21+
22+
def word_break_aux(self, s, wordDict, t):
23+
if not s:
24+
return True
25+
if s in t:
26+
return True
27+
else:
28+
for i, c in enumerate(s):
29+
word = s[:i + 1]
30+
rest = s[i + 1:]
31+
if word in wordDict and self.wordBreak(rest, wordDict):
32+
t.add(s)
33+
print(t)
34+
return True
35+
return False

0 commit comments

Comments
 (0)