-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|