Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge homework #832

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Week_03/id_137/LeetCode_104_137.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/"
"Author : Shaodong Song"
"Date : 2019-05-17"

"""
Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

3
/ \
9 20
/ \
15 7
return its depth = 3.

"""

# 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 maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""

if root == None: return 0

return max((self.maxDepth(root.left),self.maxDepth(root.right))) + 1


root = TreeNode(3)

root.left = TreeNode(9)
root.right = TreeNode(20)

right_root = root.right
right_root.left = TreeNode(15)
right_root.right = TreeNode(7)

""" case 2:
root.left = TreeNode(2)
root.right = TreeNode(2)
"""

so = Solution()

print "The binary maximum depth = %d " % so.maxDepth(root)



50 changes: 50 additions & 0 deletions Week_03/id_137/LeetCode_703_137.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/"
"Author : Shaodong Song"
"Date : 2019-05-17"

"""
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.

"""

import heapq
class KthLargest(object):

def __init__(self, k, nums):
"""
:type k: int
:type nums: List[int]
"""
self.queues = nums
self.k = k
heapq.heapify(self.queues)

while len(self.queues) > k:
heapq.heappop(self.queues)

def add(self, val):
"""
:type val: int
:rtype: int
"""
if len(self.queues) < self.k:
heapq.heappush(self.queues, val)
elif val > self.queues[0]:
"self.queues[0] is mininum val"
heapq.heapreplace(self.queues, val)

return self.queues[0]


k = 3;
arr = [4,5,8,2]
kthLargest = KthLargest(3, arr)

print "add element 3, the 3th largest element = %d" % kthLargest.add(3)
print "add element 5, the 3th largest element = %d" % kthLargest.add(5)
print "add element 10, the 3th largest element = %d" % kthLargest.add(10)



53 changes: 53 additions & 0 deletions Week_04/id_137/LeetCode_169_137.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"https://leetcode-cn.com/problems/majority-element/"
"Author : Shaodong Song"
"Date : 2019-05-18"

"""
Given an array of size n, find the majority element. The majority element is the element that appears more than [ n/2 ] times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:
Input: [3,2,3]
Output: 3

Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2

"""
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = 1
majority = nums[0]
k = len(nums) // 2

for i in range(1, len(nums)):
if cnt == 0:
majority = nums[i]
cnt += 1
else:
if majority == nums[i]:
cnt += 1
if cnt > k:
return majority
else:
cnt -= 1

return majority

so = Solution()

numbers1 = [3,2,3]
numbers2 = [2,2,1,1,1,2,2]

print "The majority element is = %d " % so.majorityElement(numbers1)
print "The majority element is = %d " % so.majorityElement(numbers2)




46 changes: 46 additions & 0 deletions Week_04/id_137/LeetCode_746_137.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"https://leetcode-cn.com/problems/min-cost-climbing-stairs/"
"Author : Shaodong Song"
"Date : 2019-05-18"

"""
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:
Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
Example 2:
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
"""

class Solution(object):
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""

length = len(cost)
dp = [0 for i in range(length)]

dp[0] = cost[0]
dp[1] = cost[1]

for i in range(2, length):
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]

return min(dp[length - 1], dp[length - 2])

so = Solution()

cost = [10, 15, 20]
cost1 = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]

print "minCostClimbingStairs = %d" % so.minCostClimbingStairs(cost)
print "minCostClimbingStairs = %d" % so.minCostClimbingStairs(cost1)