Skip to content

Commit dfbd2c3

Browse files
committed
add complexity
1 parent d39f1ac commit dfbd2c3

File tree

483 files changed

+1267
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

483 files changed

+1267
-25
lines changed

Python/01-matrix.py

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
except NameError:
99
xrange = range # Python 3
1010

11+
1112
class Solution(object):
1213
def updateMatrix(self, matrix):
1314
"""
@@ -35,6 +36,10 @@ def updateMatrix(self, matrix):
3536

3637
return matrix
3738

39+
40+
# Time: O(m * n)
41+
# Space: O(m * n)
42+
# dp solution
3843
class Solution2(object):
3944
def updateMatrix(self, matrix):
4045
"""

Python/1-bit-and-2-bit-characters.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def isOneBitCharacter(self, bits):
1112
"""

Python/132-pattern.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def find132pattern(self, nums):
1112
"""

Python/24-game.py

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
except NameError:
1010
xrange = range # Python 3
1111

12+
1213
class Solution(object):
1314
def judgePoint24(self, nums):
1415
"""
@@ -33,6 +34,9 @@ def judgePoint24(self, nums):
3334
next_nums.pop()
3435
return False
3536

37+
38+
# Time: O(n^3 * 4^n) = O(1), n = 4
39+
# Space: O(n^2) = O(1)
3640
class Solution2(object):
3741
def judgePoint24(self, nums):
3842
"""

Python/3sum.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import collections
55

6+
67
class Solution(object):
78
def threeSum(self, nums):
89
"""

Python/4-keys-keyboard.py

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def maxA(self, N):
1112
"""
@@ -25,6 +26,9 @@ def maxA(self, N):
2526
n4 = n - n3
2627
return 3**n3 * 4**n4
2728

29+
30+
# Time: O(n)
31+
# Space: O(1)
2832
class Solution2(object):
2933
def maxA(self, N):
3034
"""

Python/4sum-ii.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import collections
55

6+
67
class Solution(object):
78
def fourSumCount(self, A, B, C, D):
89
"""

Python/4sum.py

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
except NameError:
99
xrange = range # Python 3
1010

11+
12+
# Two pointer solution. (1356ms)
1113
class Solution(object):
1214
def fourSum(self, nums, target):
1315
"""
@@ -40,6 +42,10 @@ def fourSum(self, nums, target):
4042
left += 1
4143
return res
4244

45+
46+
# Time: O(n^2 * p)
47+
# Space: O(n^2 * p)
48+
# Hash solution. (224ms)
4349
class Solution2(object):
4450
def fourSum(self, nums, target):
4551
"""
@@ -70,6 +76,9 @@ def fourSum(self, nums, target):
7076
result.append(quad)
7177
return result
7278

79+
80+
# Time: O(n^2 * p) ~ O(n^4)
81+
# Space: O(n^2)
7382
class Solution3(object):
7483
def fourSum(self, nums, target):
7584
"""

Python/accounts-merge.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
except NameError:
1010
xrange = range # Python 3
1111

12+
1213
class UnionFind(object):
1314
def __init__(self):
1415
self.set = []
@@ -27,6 +28,7 @@ def union_set(self, x, y):
2728
if x_root != y_root:
2829
self.set[min(x_root, y_root)] = max(x_root, y_root)
2930

31+
3032
class Solution(object):
3133
def accountsMerge(self, accounts):
3234
"""

Python/add-and-search-word-data-structure-design.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def __init__(self):
77
self.is_string = False
88
self.leaves = {}
99

10+
1011
class WordDictionary:
1112
def __init__(self):
1213
self.root = TrieNode()
@@ -41,3 +42,4 @@ def searchHelper(self, word, start, curr):
4142

4243
return False
4344

45+

Python/add-binary.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution:
1011
# @param a, a string
1112
# @param b, a string

Python/add-bold-tag-in-string.py

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
except NameError:
1010
xrange = range # Python 3
1111

12+
13+
# 59ms
1214
class Solution(object):
1315
def addBoldTag(self, s, dict):
1416
"""
@@ -32,6 +34,10 @@ def addBoldTag(self, s, dict):
3234
result.append("</b>")
3335
return "".join(result)
3436

37+
38+
# Time: O(n * l), l is the average string length
39+
# Space: O(t) , t is the size of trie
40+
# trie solution, 439ms
3541
class Solution2(object):
3642
def addBoldTag(self, s, words):
3743
"""

Python/add-one-row-to-tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def __init__(self, x):
77
self.left = None
88
self.right = None
99

10+
1011
class Solution(object):
1112
def addOneRow(self, root, v, d):
1213
"""

Python/add-two-numbers-ii.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def __init__(self, x):
66
self.val = x
77
self.next = None
88

9+
910
class Solution(object):
1011
def addTwoNumbers(self, l1, l2):
1112
"""

Python/add-two-numbers.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def __init__(self, x):
66
self.val = x
77
self.next = None
88

9+
910
class Solution(object):
1011
def addTwoNumbers(self, l1, l2):
1112
"""

Python/additive-number.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def isAdditiveNumber(self, num):
1112
"""

Python/alien-dictionary.py

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
except NameError:
99
xrange = range # Python 3
1010

11+
12+
# BFS solution.
1113
class Solution(object):
1214
def alienOrder(self, words):
1315
"""
@@ -61,6 +63,8 @@ def findEdges(self, word1, word2, in_degree, out_degree):
6163
out_degree[word1[i]].add(word2[i])
6264
break
6365

66+
67+
# DFS solution.
6468
class Solution2(object):
6569
def alienOrder(self, words):
6670
"""

Python/all-nodes-distance-k-in-binary-tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
except NameError:
99
xrange = range # Python 3
1010

11+
1112
class Solution(object):
1213
def distanceK(self, root, target, K):
1314
"""

Python/all-oone-data-structure.py

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self, value, keys):
1111
self.prev = None
1212
self.next = None
1313

14+
1415
class LinkedList(object):
1516
def __init__(self):
1617
self.head, self.tail = Node(0, set()), Node(0, set())
@@ -40,6 +41,7 @@ def front(self):
4041
def back(self):
4142
return self.tail.prev
4243

44+
4345
class AllOne(object):
4446

4547
def __init__(self):

Python/all-possible-full-binary-trees.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ def __init__(self, x):
77
self.left = None
88
self.right = None
99

10+
1011
class Solution(object):
1112
def __init__(self):
1213
self.__memo = {1: [TreeNode(0)]}
13-
14+
1415
def allPossibleFBT(self, N):
1516
"""
1617
:type N: int
@@ -31,4 +32,5 @@ def allPossibleFBT(self, N):
3132
self.__memo[N] = result
3233

3334
return self.__memo[N]
35+
3436

Python/ambiguous-coordinates.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
except NameError:
99
xrange = range # Python 3
1010

11+
1112
class Solution(object):
1213
def ambiguousCoordinates(self, S):
1314
"""

Python/android-unlock-patterns.py

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
10+
# DP solution.
911
class Solution(object):
1012
def numberOfPatterns(self, m, n):
1113
"""
@@ -65,6 +67,10 @@ def convert(i, j):
6567

6668
return res
6769

70+
71+
# Time: O(9^2 * 2^9)
72+
# Space: O(9 * 2^9)
73+
# DP solution.
6874
class Solution2(object):
6975
def numberOfPatterns(self, m, n):
7076
"""
@@ -127,6 +133,10 @@ def convert(i, j):
127133

128134
return res
129135

136+
137+
# Time: O(9!)
138+
# Space: O(9)
139+
# Backtracking solution. (TLE)
130140
class Solution_TLE(object):
131141
def numberOfPatterns(self, m, n):
132142
"""

Python/arithmetic-slices-ii-subsequence.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
except NameError:
99
xrange = range # Python 3
1010

11+
1112
class Solution(object):
1213
def numberOfArithmeticSlices(self, A):
1314
"""

Python/arranging-coins.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import math
55

6+
67
class Solution(object):
78
def arrangeCoins(self, n):
89
"""
@@ -11,6 +12,9 @@ def arrangeCoins(self, n):
1112
"""
1213
return int((math.sqrt(8*n+1)-1) / 2) # sqrt is O(logn) time.
1314

15+
16+
# Time: O(logn)
17+
# Space: O(1)
1418
class Solution2(object):
1519
def arrangeCoins(self, n):
1620
"""

Python/array-partition-i.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def arrayPairSum(self, nums):
1112
"""
@@ -22,6 +23,9 @@ def arrayPairSum(self, nums):
2223
r = (lookup[i-LEFT] + r) % 2
2324
return result
2425

26+
27+
# Time: O(nlogn)
28+
# Space: O(1)
2529
class Solution2(object):
2630
def arrayPairSum(self, nums):
2731
"""
@@ -34,6 +38,9 @@ def arrayPairSum(self, nums):
3438
result += nums[i]
3539
return result
3640

41+
42+
# Time: O(nlogn)
43+
# Space: O(n)
3744
class Solution3(object):
3845
def arrayPairSum(self, nums):
3946
"""

Python/assign-cookies.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def findContentChildren(self, g, s):
1112
"""

Python/asteroid-collision.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def asteroidCollision(self, asteroids):
1112
"""

Python/average-of-levels-in-binary-tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except NameError:
77
xrange = range # Python 3
88

9+
910
class Solution(object):
1011
def averageOfLevels(self, root):
1112
"""

Python/backspace-string-compare.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
except NameError:
99
xrange = range # Python 3
1010

11+
1112
class Solution(object):
1213
def backspaceCompare(self, S, T):
1314
"""

Python/balanced-binary-tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def __init__(self, x):
77
self.left = None
88
self.right = None
99

10+
1011
class Solution(object):
1112
# @param root, a tree node
1213
# @return a boolean

0 commit comments

Comments
 (0)