Skip to content

Commit

Permalink
1209
Browse files Browse the repository at this point in the history
  • Loading branch information
youhusky committed Dec 10, 2017
1 parent df26271 commit d7aa3c0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 023. Merge k Sorted Lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ def mergeKLists(self, lists):
head = dummy
for node in lists:
# imp !
# O(k)
if node:
heapq.heappush(pq, (node.val, node))
while pq:
#O(n)
head.next = heapq.heappop(pq)[1]
head = head.next
if head.next:
# O(logk)
heapq.heappush(pq,(head.next.val, head.next))
return dummy.next
31 changes: 31 additions & 0 deletions 098. Validate Binary Search Tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,36 @@ def isValidBSThelper(self,root,left,right):
if left >= root.val or right <= root.val:
return False
return self.isValidBSThelper(root.left, left, root.val) and self.isValidBSThelper(root.right, root.val, right)

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
res = []
flag = [True]
self.check(root, res,flag)
print res
return True if flag[0] else False
def check(self,root,res,flag):
if not root:
return
self.check(root.left, res,flag)
if res and root.val <= res[-1]:
flag[0] = False
return
res.append(root.val)
self.check(root.right, res, flag)





28 changes: 28 additions & 0 deletions 110. Balanced Binary Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Given a binary tree, determine if it is height-balanced.

# For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isBalanced(self, root):
"""
O(n)
O(1)
:type root: TreeNode
:rtype: bool
"""
return self.check(root) != -1
def check(self, root):
if not root:
return 0

left = self.check(root.left)
right = self.check(root.right)
if left == -1 or right == -1 or abs(left-right)>1:
return -1
return max(left, right) +1
33 changes: 33 additions & 0 deletions 300. Longest Increasing Subsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def lis(nums):
n = len(nums)
# m means until this number the maxnumber of LIS
m = [0] * n
for i in range(n-2, -1, -1):
for j in range(n-1, i, -1):
# if m[i] > m[j] means there is a gap which is not continus
if nums[i] < nums[j] and m[i] <= m[j]:
m[i] += 1
max_value = max(m)
res = []
for k in range(n):
if m[k] == max_value:
res.append(nums[k])
max_value -= 1
return res
arr = [10, 22, 9, 33, 21, 50, 41, 60, 80]
print (lis(arr))

def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
dp = [1] * (len(nums) + 1)
for i in range(len(nums)):
for j in range(i):
if nums[j] < nums[i]:
# from j -> i dp[i]+1
dp[i] = max(dp[i], dp[j]+1)
return max(dp)
9 changes: 9 additions & 0 deletions unionSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def unionSet(nums):

# [(1,2),(2,3),(3,4),(5,6,7),(7,8),(8,9)]
res = [nums[0]]
for i in range(1, len(nums)):
item = res.pop(0)



0 comments on commit d7aa3c0

Please sign in to comment.