Skip to content

Commit bc00d87

Browse files
committed
Adding solution of 30, 44, 45, 46, 48, 54, 56, 57, 60 id problems
1 parent 0c8f2ed commit bc00d87

File tree

9 files changed

+340
-0
lines changed

9 files changed

+340
-0
lines changed

30.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'''
2+
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
3+
4+
Example 1:
5+
6+
Input:
7+
s = "barfoothefoobarman",
8+
words = ["foo","bar"]
9+
Output: [0,9]
10+
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
11+
The output order does not matter, returning [9,0] is fine too.
12+
Example 2:
13+
14+
Input:
15+
s = "wordgoodstudentgoodword",
16+
words = ["word","student"]
17+
Output: []
18+
'''
19+
20+
class Solution(object):
21+
def findSubstring(self, s, words):
22+
"""
23+
:type s: str
24+
:type words: List[str]
25+
:rtype: List[int]
26+
"""
27+
if not str or not words:
28+
return []
29+
30+
counts = {}
31+
for word in words:
32+
if word in counts:
33+
counts[word] += 1
34+
else:
35+
counts[word] = 1
36+
37+
result = []
38+
n, numOfWords, fixLen = len(s), len(words),len(words[0])
39+
40+
for index in range(0, n-numOfWords*fixLen+1):
41+
seen = {}
42+
43+
index_j = 0
44+
while index_j < numOfWords:
45+
word = s[index + index_j*fixLen: index + (index_j+1)*fixLen]
46+
if word in counts:
47+
if word in seen:
48+
seen[word] += 1
49+
else:
50+
seen[word] = 1
51+
52+
if seen[word] > counts[word]:
53+
break
54+
else:
55+
break
56+
index_j += 1
57+
58+
if index_j == numOfWords:
59+
result.append(index)
60+
61+
return
62+
63+
# Time: O(N^2)
64+
# Space: O(N)

44.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.
3+
4+
'?' Matches any single character.
5+
'*' Matches any sequence of characters (including the empty sequence).
6+
The matching should cover the entire input string (not partial).
7+
8+
Note:
9+
10+
s could be empty and contains only lowercase letters a-z.
11+
p could be empty and contains only lowercase letters a-z, and characters like ? or *.
12+
'''
13+
14+
class Solution(object):
15+
def isMatch(self, s, p):
16+
"""
17+
:type s: str
18+
:type p: str
19+
:rtype: bool
20+
"""
21+
22+
if len(p) == 0:
23+
return len(s) == 0
24+
25+
dp = [[False for _ in range(len(p) + 1)] for _ in range(len(s) + 1)]
26+
dp[0][0] = True
27+
28+
for index in range(1,len(dp[0])):
29+
if p[index-1] == '*':
30+
dp[0][index] = dp[0][index-1]
31+
32+
for index_i in range(1, len(dp)):
33+
for index_j in range(1, len(dp[0])):
34+
if s[index_i - 1] == p[index_j - 1] or p[index_j - 1] == '?':
35+
dp[index_i][index_j] = dp[index_i-1][index_j-1]
36+
elif p[index_j-1] == '*':
37+
dp[index_i][index_j] = dp[index_i][index_j-1] or dp[index_i-1][index_j]
38+
else:
39+
dp[index_i][index_j] = False
40+
return dp[len(s)][len(p)]

45.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
3+
4+
Each element in the array represents your maximum jump length at that position.
5+
6+
Your goal is to reach the last index in the minimum number of jumps.
7+
8+
Example:
9+
10+
Input: [2,3,1,1,4]
11+
Output: 2
12+
Explanation: The minimum number of jumps to reach the last index is 2.
13+
Jump 1 step from index 0 to 1, then 3 steps to the last index.
14+
15+
16+
'''
17+
class Solution(object):
18+
def jump(self, nums):
19+
"""
20+
:type nums: List[int]
21+
:rtype: int
22+
"""
23+
steps, lastRearch, maxReach = 0, 0 ,0
24+
25+
for index in range(len(nums)):
26+
if index > lastRearch:
27+
lastRearch = maxReach
28+
steps += 1
29+
maxReach = max(maxReach, index + nums[index])
30+
31+
return steps if maxReach == len(nums) - 1 else 0

46.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def permute(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[List[int]]
6+
"""
7+
8+
if len(nums) == 0:
9+
return []
10+
if len(nums) == 1:
11+
return [nums]
12+
13+
result = []
14+
for index in range(len(nums)):
15+
for p in self.permute(nums[0:index] + nums[index+1:]):
16+
result.append([nums[index]] + p)
17+
18+
return result

48.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
You are given an n x n 2D matrix representing an image.
3+
4+
Rotate the image by 90 degrees (clockwise).
5+
'''
6+
class Solution(object):
7+
def rotate(self, matrix):
8+
"""
9+
:type matrix: List[List[int]]
10+
:rtype: void Do not return anything, modify matrix in-place instead.
11+
"""
12+
n = len(matrix)
13+
if n%2 == 0:
14+
m = n/2
15+
else:
16+
m = n/2 + 1
17+
18+
for i in range(n/2):
19+
for j in range(m):
20+
temp = matrix[i][j]
21+
matrix[i][j] = matrix[n-j-1][i]
22+
matrix[n-j-1][i] = matrix[n-i-1][n-j-1]
23+
matrix[n-i-1][n-j-1] = matrix[j][n-i-1]
24+
matrix[j][n-i-1] = temp

54.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
3+
4+
Example 1:
5+
6+
Input:
7+
[
8+
[ 1, 2, 3 ],
9+
[ 4, 5, 6 ],
10+
[ 7, 8, 9 ]
11+
]
12+
Output: [1,2,3,6,9,8,7,4,5]
13+
'''
14+
15+
class Solution(object):
16+
def spiralOrder(self, matrix):
17+
"""
18+
:type matrix: List[List[int]]
19+
:rtype: List[int]
20+
"""
21+
if not matrix:
22+
return []
23+
24+
R, C = len(matrix), len(matrix[0])
25+
dr = [0, 1, 0, -1]
26+
dc = [1, 0, -1, 0]
27+
28+
result = []
29+
seen = [[False]*C for _ in range(R)]
30+
row = 0
31+
col = 0
32+
di = 0
33+
for _ in range(R*C):
34+
result.append(matrix[row][col])
35+
seen[row][col] = True
36+
rr, cc = row + dr[di], col + dc[di]
37+
if 0 <= rr < R and 0 <= cc < C and not seen[rr][cc]:
38+
row, col = rr, cc
39+
else:
40+
di = (di+1)%4
41+
row, col = row + dr[di], col + dc[di]
42+
43+
return result

56.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Given a collection of intervals, merge all overlapping intervals.
3+
4+
Example 1:
5+
6+
Input: [[1,3],[2,6],[8,10],[15,18]]
7+
Output: [[1,6],[8,10],[15,18]]
8+
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
9+
Example 2:
10+
11+
Input: [[1,4],[4,5]]
12+
Output: [[1,5]]
13+
Explanation: Intervals [1,4] and [4,5] are considerred overlapping.
14+
'''
15+
16+
# Definition for an interval.
17+
# class Interval(object):
18+
# def __init__(self, s=0, e=0):
19+
# self.start = s
20+
# self.end = e
21+
22+
class compare(object):
23+
def __init__(self, interval):
24+
self.interval = interval
25+
26+
def __lt__(self, other):
27+
if self.interval.start == other.interval.start:
28+
return self.interval.end < other.interval.end
29+
return self.interval.start < other.interval.end
30+
31+
class Solution(object):
32+
def merge(self, intervals):
33+
"""
34+
:type intervals: List[Interval]
35+
:rtype: List[Interval]
36+
"""
37+
38+
intervals = sorted(intervals, key= compare)
39+
# intervals.sort(key=lambda x: x.start)
40+
merged = []
41+
for interval in intervals:
42+
if not merged or merged[-1].end < interval.start:
43+
merged.append(interval)
44+
else:
45+
merged[-1].end = max(merged[-1].end, interval.end)
46+
return merged
47+
48+
# Time: O(N*log(N))
49+
# Space: O(1)

57.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
3+
4+
You may assume that the intervals were initially sorted according to their start times.
5+
6+
Example 1:
7+
8+
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
9+
Output: [[1,5],[6,9]]
10+
Example 2:
11+
12+
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
13+
Output: [[1,2],[3,10],[12,16]]
14+
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
15+
'''
16+
17+
# Definition for an interval.
18+
# class Interval(object):
19+
# def __init__(self, s=0, e=0):
20+
# self.start = s
21+
# self.end = e
22+
23+
class Solution(object):
24+
def insert(self, intervals, newInterval):
25+
"""
26+
:type intervals: List[Interval]
27+
:type newInterval: Interval
28+
:rtype: List[Interval]
29+
"""
30+
result = []
31+
for interval in intervals:
32+
if newInterval.start > interval.end:
33+
result.append(interval)
34+
elif newInterval.end < interval.start:
35+
result.append(newInterval)
36+
newInterval = interval
37+
elif newInterval.start <= interval.end or newInterval.end >= interval.start:
38+
newInterval = Interval(min(newInterval.start, interval.start), max(interval.end, newInterval.end))
39+
40+
result.append(newInterval)
41+
return result

60.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def getPermutation(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: str
7+
"""
8+
9+
nums = []
10+
for index in range(1, n+1):
11+
nums.append(index)
12+
13+
def permute(nums):
14+
if len(nums) == 0:
15+
return []
16+
if len(nums) == 1:
17+
return [nums]
18+
19+
result = []
20+
for index in range(len(nums)):
21+
for p in permute(nums[0:index] + nums[index+1:]):
22+
result.append([nums[index]] + p)
23+
24+
return result
25+
26+
value = permute(nums)[k-1]
27+
result = ""
28+
for val in value:
29+
result += str(val)
30+
return result

0 commit comments

Comments
 (0)