Skip to content

Commit

Permalink
add complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Oct 12, 2018
1 parent d39f1ac commit dfbd2c3
Show file tree
Hide file tree
Showing 483 changed files with 1,267 additions and 25 deletions.
5 changes: 5 additions & 0 deletions Python/01-matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def updateMatrix(self, matrix):
"""
Expand Down Expand Up @@ -35,6 +36,10 @@ def updateMatrix(self, matrix):

return matrix


# Time: O(m * n)
# Space: O(m * n)
# dp solution
class Solution2(object):
def updateMatrix(self, matrix):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/1-bit-and-2-bit-characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def isOneBitCharacter(self, bits):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/132-pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def find132pattern(self, nums):
"""
Expand Down
4 changes: 4 additions & 0 deletions Python/24-game.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def judgePoint24(self, nums):
"""
Expand All @@ -33,6 +34,9 @@ def judgePoint24(self, nums):
next_nums.pop()
return False


# Time: O(n^3 * 4^n) = O(1), n = 4
# Space: O(n^2) = O(1)
class Solution2(object):
def judgePoint24(self, nums):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/3sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import collections


class Solution(object):
def threeSum(self, nums):
"""
Expand Down
4 changes: 4 additions & 0 deletions Python/4-keys-keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def maxA(self, N):
"""
Expand All @@ -25,6 +26,9 @@ def maxA(self, N):
n4 = n - n3
return 3**n3 * 4**n4


# Time: O(n)
# Space: O(1)
class Solution2(object):
def maxA(self, N):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/4sum-ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import collections


class Solution(object):
def fourSumCount(self, A, B, C, D):
"""
Expand Down
9 changes: 9 additions & 0 deletions Python/4sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
except NameError:
xrange = range # Python 3


# Two pointer solution. (1356ms)
class Solution(object):
def fourSum(self, nums, target):
"""
Expand Down Expand Up @@ -40,6 +42,10 @@ def fourSum(self, nums, target):
left += 1
return res


# Time: O(n^2 * p)
# Space: O(n^2 * p)
# Hash solution. (224ms)
class Solution2(object):
def fourSum(self, nums, target):
"""
Expand Down Expand Up @@ -70,6 +76,9 @@ def fourSum(self, nums, target):
result.append(quad)
return result


# Time: O(n^2 * p) ~ O(n^4)
# Space: O(n^2)
class Solution3(object):
def fourSum(self, nums, target):
"""
Expand Down
2 changes: 2 additions & 0 deletions Python/accounts-merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
except NameError:
xrange = range # Python 3


class UnionFind(object):
def __init__(self):
self.set = []
Expand All @@ -27,6 +28,7 @@ def union_set(self, x, y):
if x_root != y_root:
self.set[min(x_root, y_root)] = max(x_root, y_root)


class Solution(object):
def accountsMerge(self, accounts):
"""
Expand Down
2 changes: 2 additions & 0 deletions Python/add-and-search-word-data-structure-design.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def __init__(self):
self.is_string = False
self.leaves = {}


class WordDictionary:
def __init__(self):
self.root = TrieNode()
Expand Down Expand Up @@ -41,3 +42,4 @@ def searchHelper(self, word, start, curr):

return False


1 change: 1 addition & 0 deletions Python/add-binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution:
# @param a, a string
# @param b, a string
Expand Down
6 changes: 6 additions & 0 deletions Python/add-bold-tag-in-string.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
except NameError:
xrange = range # Python 3


# 59ms
class Solution(object):
def addBoldTag(self, s, dict):
"""
Expand All @@ -32,6 +34,10 @@ def addBoldTag(self, s, dict):
result.append("</b>")
return "".join(result)


# Time: O(n * l), l is the average string length
# Space: O(t) , t is the size of trie
# trie solution, 439ms
class Solution2(object):
def addBoldTag(self, s, words):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/add-one-row-to-tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def __init__(self, x):
self.left = None
self.right = None


class Solution(object):
def addOneRow(self, root, v, d):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/add-two-numbers-ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def __init__(self, x):
self.val = x
self.next = None


class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/add-two-numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def __init__(self, x):
self.val = x
self.next = None


class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/additive-number.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def isAdditiveNumber(self, num):
"""
Expand Down
4 changes: 4 additions & 0 deletions Python/alien-dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
except NameError:
xrange = range # Python 3


# BFS solution.
class Solution(object):
def alienOrder(self, words):
"""
Expand Down Expand Up @@ -61,6 +63,8 @@ def findEdges(self, word1, word2, in_degree, out_degree):
out_degree[word1[i]].add(word2[i])
break


# DFS solution.
class Solution2(object):
def alienOrder(self, words):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/all-nodes-distance-k-in-binary-tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def distanceK(self, root, target, K):
"""
Expand Down
2 changes: 2 additions & 0 deletions Python/all-oone-data-structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self, value, keys):
self.prev = None
self.next = None


class LinkedList(object):
def __init__(self):
self.head, self.tail = Node(0, set()), Node(0, set())
Expand Down Expand Up @@ -40,6 +41,7 @@ def front(self):
def back(self):
return self.tail.prev


class AllOne(object):

def __init__(self):
Expand Down
4 changes: 3 additions & 1 deletion Python/all-possible-full-binary-trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ def __init__(self, x):
self.left = None
self.right = None


class Solution(object):
def __init__(self):
self.__memo = {1: [TreeNode(0)]}

def allPossibleFBT(self, N):
"""
:type N: int
Expand All @@ -31,4 +32,5 @@ def allPossibleFBT(self, N):
self.__memo[N] = result

return self.__memo[N]


1 change: 1 addition & 0 deletions Python/ambiguous-coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def ambiguousCoordinates(self, S):
"""
Expand Down
10 changes: 10 additions & 0 deletions Python/android-unlock-patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
except NameError:
xrange = range # Python 3


# DP solution.
class Solution(object):
def numberOfPatterns(self, m, n):
"""
Expand Down Expand Up @@ -65,6 +67,10 @@ def convert(i, j):

return res


# Time: O(9^2 * 2^9)
# Space: O(9 * 2^9)
# DP solution.
class Solution2(object):
def numberOfPatterns(self, m, n):
"""
Expand Down Expand Up @@ -127,6 +133,10 @@ def convert(i, j):

return res


# Time: O(9!)
# Space: O(9)
# Backtracking solution. (TLE)
class Solution_TLE(object):
def numberOfPatterns(self, m, n):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/arithmetic-slices-ii-subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def numberOfArithmeticSlices(self, A):
"""
Expand Down
4 changes: 4 additions & 0 deletions Python/arranging-coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import math


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


# Time: O(logn)
# Space: O(1)
class Solution2(object):
def arrangeCoins(self, n):
"""
Expand Down
7 changes: 7 additions & 0 deletions Python/array-partition-i.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def arrayPairSum(self, nums):
"""
Expand All @@ -22,6 +23,9 @@ def arrayPairSum(self, nums):
r = (lookup[i-LEFT] + r) % 2
return result


# Time: O(nlogn)
# Space: O(1)
class Solution2(object):
def arrayPairSum(self, nums):
"""
Expand All @@ -34,6 +38,9 @@ def arrayPairSum(self, nums):
result += nums[i]
return result


# Time: O(nlogn)
# Space: O(n)
class Solution3(object):
def arrayPairSum(self, nums):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/assign-cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def findContentChildren(self, g, s):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/asteroid-collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def asteroidCollision(self, asteroids):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/average-of-levels-in-binary-tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def averageOfLevels(self, root):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/backspace-string-compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def backspaceCompare(self, S, T):
"""
Expand Down
1 change: 1 addition & 0 deletions Python/balanced-binary-tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def __init__(self, x):
self.left = None
self.right = None


class Solution(object):
# @param root, a tree node
# @return a boolean
Expand Down
Loading

0 comments on commit dfbd2c3

Please sign in to comment.