Letter Combinations of a Phone Number

Letter Combinations of a Phone Number

Binary Search Version - 36ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits: return []
res = ['']
nums = list(digits)
d = {'2': ['a', 'b', 'c'], '3': ['d', 'e', 'f'], '4': ['g', 'h', 'i'], '5': ['j', 'k', 'l'], '6': ['m', 'n', 'o'
], '7': ['p', 'q', 'r', 's'], '8': ['t', 'u', 'v'], '9': ['w', 'x', 'y', 'z']}
for n in digits:
lst = d[n]
new_res = []
for char in lst:
for string in res:
new_res.append(string + char)
res = new_res
return res

test code

1
2
3
4
In [347]: s = Solution(); t = s.letterCombinations('23')
In [349]: print(t)
['ad', 'bd', 'cd', 'ae', 'be', 'ce', 'af', 'bf', 'cf']

leet code

Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal

Binary Search Version - 36ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def zigzagLevelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
queue = [root]
res = []
level = 0
while queue:
level += 1
temp = []
tempq = []
for node in queue:
temp.append(node.val)
if node.left:
tempq.append(node.left)
if node.right:
tempq.append(node.right)
queue = tempq
if level % 2 == 0 :
temp = temp[::-1]
res.append(temp)
return res

leet code

Perfect Squares

Perfect Squares

Binary Search Version - 356ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import math
class Solution:
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
s = [i*i for i in range(1,int(math.sqrt(n))+1)] # Square numbers <= n
l = 0 # BFS level
currentLevel = [0] # List of numbers in BFS level l
while True:
nextLevel = []
for a in currentLevel:
for b in s:
if a+b == n: return l+1 # Found n
if a+b < n: nextLevel.append(a+b)
currentLevel = list(set(nextLevel)) # Remove duplicates
l += 1

test code

1
2
In [315]: s = Solution(); t = s.numSquares(11); print(t)
3

leet code

Increasing Triplet Subsequence

Increasing Triplet Subsequence

Binary Search Version - 40ms O(n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
first = second = float('inf')
for x in nums:
if x <= first:
first = x
elif x <= second:
second = x
else:
return True
return False

test code

1
2
3
4
5
In [306]: s = Solution(); t = s.increasingTriplet(n); print(t)
True
In [307]: n
Out[307]: [1, 3, 6, 7, 9, 4, 10, 5, 6]

leet code

Longest Increasing Subsequence

Longest Increasing Subsequence

Dp Version - 1596ms O(n2)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
dp = [1] * len(nums)
for i in range(len(nums)):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)

Binary Search Version - 48ms O(nlogn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
tails = [0] * len(nums)
size = 0
for x in nums:
i, j = 0, size
while i != j:
m = (i + j) // 2
if tails[m] < x:
i = m + 1
else:
j = m
print(i, j, size, x)
tails[i] = x
size = max(i + 1, size)
print(size, tails)
return size

test code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
In [250]: n = [1,3,6,7,9,4,10,5,6]
In [253]: s = Solution(); t = s.lengthOfLIS(n); print(t)
6
In [264]: s = Solution(); t = s.lengthOfLIS(n); print(t)
0 0 0 1
1 [1, 0, 0, 0, 0, 0, 0, 0, 0]
1 1 1 3
2 [1, 3, 0, 0, 0, 0, 0, 0, 0]
2 2 2 6
3 [1, 3, 6, 0, 0, 0, 0, 0, 0]
3 3 3 7
4 [1, 3, 6, 7, 0, 0, 0, 0, 0]
4 4 4 9
5 [1, 3, 6, 7, 9, 0, 0, 0, 0]
2 2 5 4
5 [1, 3, 4, 7, 9, 0, 0, 0, 0]
5 5 5 10
6 [1, 3, 4, 7, 9, 10, 0, 0, 0]
3 3 6 5
6 [1, 3, 4, 5, 9, 10, 0, 0, 0]
4 4 6 6
6 [1, 3, 4, 5, 6, 10, 0, 0, 0]
6

leet code

Java/Python Binary search O(nlogn) time with explanation-time-with-explanation)