Skip to content

Commit 4ae07ef

Browse files
committed
Adding solutions of 200, 203, 206, 208, 210 and top interview questions
1 parent 5748a12 commit 4ae07ef

21 files changed

+755
-2
lines changed

100-200q/125.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ def numDistinct(self, s, t):
2525
dp[r][c] = dp[r-1][c]
2626
if s[r-1] == t[c-1]:
2727
dp[r][c] += dp[r-1][c-1]
28-
return dp[row][col]
28+
return dp[row][col]
29+
30+
# Time: O(N^2)
31+
# Space: O(N^2)

100-200q/127.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
3+
4+
Only one letter can be changed at a time.
5+
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
6+
Note:
7+
8+
Return 0 if there is no such transformation sequence.
9+
All words have the same length.
10+
All words contain only lowercase alphabetic characters.
11+
You may assume no duplicates in the word list.
12+
You may assume beginWord and endWord are non-empty and are not the same.
13+
Example 1:
14+
15+
Input:
16+
beginWord = "hit",
17+
endWord = "cog",
18+
wordList = ["hot","dot","dog","lot","log","cog"]
19+
20+
Output: 5
21+
22+
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
23+
return its length 5.
24+
'''
25+
26+
class Solution(object):
27+
def ladderLength(self, beginWord, endWord, wordList):
28+
"""
29+
:type beginWord: str
30+
:type endWord: str
31+
:type wordList: List[str]
32+
:rtype: int
33+
"""
34+
d = {}
35+
for word in wordList:
36+
for i in range(len(word)):
37+
s = word[:i] + "_" + word[i+1:]
38+
if s in d:
39+
d[s].append(word)
40+
else:
41+
d[s] = [word]
42+
43+
queue, visited = [], set()
44+
queue.append((beginWord, 1))
45+
while queue:
46+
word, steps = queue.pop(0)
47+
if word not in visited:
48+
visited.add(word)
49+
50+
if word == endWord:
51+
return steps
52+
else:
53+
for index in range(len(word)):
54+
s = word[:index] + "_" + word[index+1:]
55+
neigh_words = []
56+
if s in d:
57+
neigh_words = d[s]
58+
59+
for neigh in neigh_words:
60+
if neigh not in visited:
61+
queue.append((neigh, steps+1))
62+
return 0

100-200q/128.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
3+
4+
Your algorithm should run in O(n) complexity.
5+
6+
Example:
7+
8+
Input: [100, 4, 200, 1, 3, 2]
9+
Output: 4
10+
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
11+
'''
12+
13+
class Solution(object):
14+
def longestConsecutive(self, nums):
15+
"""
16+
:type nums: List[int]
17+
:rtype: int
18+
"""
19+
result = 0
20+
nums = set(nums)
21+
22+
for num in nums:
23+
if num-1 not in nums:
24+
curr = num
25+
length = 1
26+
27+
while curr+1 in nums:
28+
curr += 1
29+
length += 1
30+
result = max(result, length)
31+
return result

100-200q/129.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
3+
4+
An example is the root-to-leaf path 1->2->3 which represents the number 123.
5+
6+
Find the total sum of all root-to-leaf numbers.
7+
8+
Note: A leaf is a node with no children.
9+
10+
Example:
11+
12+
Input: [1,2,3]
13+
1
14+
/ \
15+
2 3
16+
Output: 25
17+
Explanation:
18+
The root-to-leaf path 1->2 represents the number 12.
19+
The root-to-leaf path 1->3 represents the number 13.
20+
Therefore, sum = 12 + 13 = 25.
21+
'''
22+
23+
# Definition for a binary tree node.
24+
# class TreeNode(object):
25+
# def __init__(self, x):
26+
# self.val = x
27+
# self.left = None
28+
# self.right = None
29+
30+
class Solution(object):
31+
def sumNumbers(self, root):
32+
"""
33+
:type root: TreeNode
34+
:rtype: int
35+
"""
36+
if not root:
37+
return 0
38+
39+
def dfs(root, num, total):
40+
if not root:
41+
return total
42+
num = num*10 + root.val
43+
if not root.left and not root.right:
44+
total += num
45+
return total
46+
47+
return dfs(root.left, num) + dfs(root.right, num)
48+
49+
return dfs(root, 0, 0)

100-200q/130.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
3+
4+
A region is captured by flipping all 'O's into 'X's in that surrounded region.
5+
6+
Example:
7+
8+
X X X X
9+
X O O X
10+
X X O X
11+
X O X X
12+
After running your function, the board should be:
13+
14+
X X X X
15+
X X X X
16+
X X X X
17+
X O X X
18+
'''
19+
20+
class Solution(object):
21+
def solve(self, board):
22+
"""
23+
:type board: List[List[str]]
24+
:rtype: void Do not return anything, modify board in-place instead.
25+
"""
26+

100-200q/131.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
Given a string s, partition s such that every substring of the partition is a palindrome.
3+
4+
Return all possible palindrome partitioning of s.
5+
'''
6+
7+
class Solution(object):
8+
def partition(self, s):
9+
result = []
10+
def valid(s):
11+
for i in range(len(s)/2):
12+
if s[i] != s[-(i+1)]:
13+
return False
14+
return True
15+
16+
def partitionRec(curr, s, i):
17+
if i == len(s):
18+
result.append(curr)
19+
else:
20+
for j in range(i, len(s)):
21+
if valid(s[i:j+1]):
22+
partitionRec(curr + [s[i:j+1]], s, j+1)
23+
24+
partitionRec([], s, 0)
25+
return result

100-200q/134.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
3+
4+
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
5+
6+
Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
7+
'''
8+
9+
class Solution(object):
10+
def canCompleteCircuit(self, gas, cost):
11+
"""
12+
:type gas: List[int]
13+
:type cost: List[int]
14+
:rtype: int
15+
"""
16+
start, curr_sum, total_sum =0, 0, 0
17+
for index in range(len(gas)):
18+
diff = gas[index] - cost[index]
19+
total_sum += diff
20+
curr_sum += diff
21+
22+
if curr_sum < 0:
23+
start = index + 1
24+
curr_sum = 0
25+
26+
if total_sum >= 0:
27+
return start
28+
return -1

100-200q/139.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ def wordBreak(self, s, wordDict):
2525
for j in range(i, -1, -1):
2626
if dp[j] and s[j:i+1] in wordDict:
2727
dp[i+1] = True
28-
wordBreak
28+
break
2929
return dp[len(s)]

100-200q/141.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Given a linked list, determine if it has a cycle in it.
3+
4+
Follow up:
5+
Can you solve it without using extra space?
6+
'''
7+
8+
# Definition for singly-linked list.
9+
# class ListNode(object):
10+
# def __init__(self, x):
11+
# self.val = x
12+
# self.next = None
13+
14+
class Solution(object):
15+
def hasCycle(self, head):
16+
"""
17+
:type head: ListNode
18+
:rtype: bool
19+
"""
20+
21+
if not head:
22+
return False
23+
24+
slow, fast = head,head
25+
while fast and fast.next:
26+
slow = slow.next
27+
fast = fast.next.next
28+
if slow == fast:
29+
return True
30+
return False

100-200q/148.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
Sort a linked list in O(n log n) time using constant space complexity.
3+
4+
Example 1:
5+
6+
Input: 4->2->1->3
7+
Output: 1->2->3->4
8+
'''
9+
10+
# Definition for singly-linked list.
11+
# class ListNode(object):
12+
# def __init__(self, x):
13+
# self.val = x
14+
# self.next = None
15+
16+
class Solution(object):
17+
def sortList(self, head):
18+
"""
19+
:type head: ListNode
20+
:rtype: ListNode
21+
"""
22+
23+
if not head or not head.next:
24+
return head
25+
26+
slow, fast = head, head.next
27+
28+
while fast.next and fast.next.next:
29+
slow = slow.next
30+
fast = fast.next.next
31+
32+
head1, head2 = head, slow.next
33+
slow.next = None
34+
head1 = self.sortList(head1)
35+
head2 = self.sortList(head2)
36+
head = self.merge(head1, head2)
37+
return head
38+
39+
def merge(self, head1, head2):
40+
if not head1:
41+
return head2
42+
if not head2:
43+
return head1
44+
45+
result = ListNode(0)
46+
p = result
47+
48+
while head1 and head2:
49+
if head1.val <= head2.val:
50+
p.next = ListNode(head1.val)
51+
head1 = head1.next
52+
p = p.next
53+
else:
54+
p.next = ListNode(head2.val)
55+
head2 = head2.next
56+
p = p.next
57+
58+
if head1:
59+
p.next = head1
60+
if head2:
61+
p.next = head2
62+
return result.next

0 commit comments

Comments
 (0)