Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
172,173,174,179
  • Loading branch information
selfboot committed Dec 8, 2015
1 parent 765979c commit af3c205
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
53 changes: 53 additions & 0 deletions DynamicProgramming/174_DungeonGame.py
@@ -0,0 +1,53 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-


class Solution(object):
def calculateMinimumHP(self, dungeon):
""" Dynamic Programming
dp[i][j]: the leatest health point must remain before enter room [i, j]
Initinal: After enter the princess's room,
knight's health point should at least be 1.
that's say, dp[m_rows-1][n_cols-1] = 1 or 1-demon's hurt
Then assume the knight go from princess's room(right_down) to up_left.
DP process: There are two ways to enter to room [i, j], dp[i][j] =
1. right to left: dp[i][j+1] - dungeon[i][j] or 1
2. down to up: dp[i+1][j] - dungeon[i][j] or 1
"""
m_rows = len(dungeon)
n_cols = len(dungeon[0])
dp = [[1 for i in range(n_cols)] for j in range(m_rows)]
if dungeon[-1][-1] > 0:
dp[-1][-1] = 1
else:
dp[-1][-1] = 1 - dungeon[-1][-1]

for i in range(m_rows-1, -1, -1):
for j in range(n_cols-1, -1, -1):
down_up = None
right_left = None
if i+1 < m_rows:
if dungeon[i][j] >= dp[i+1][j]:
down_up = 1
else:
down_up = dp[i+1][j] - dungeon[i][j]
if j+1 < n_cols:
if dungeon[i][j] >= dp[i][j+1]:
right_left = 1
else:
right_left = dp[i][j+1] - dungeon[i][j]
if right_left and down_up:
dp[i][j] = min(down_up, right_left)
elif right_left or down_up:
dp[i][j] = down_up or right_left
else:
pass

return dp[0][0]
"""
[[0]]
[[-1]]
[[0,-3]]
[[-2,-3,-3], [-5,-10,1], [10,30,-5]]
"""
25 changes: 25 additions & 0 deletions Math/172_FactorialTrailingZeroes.py
@@ -0,0 +1,25 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-


class Solution(object):
def trailingZeroes(self, n):
if n < 5:
return 0

sums = 0
i = 1
# Every five numbers will produce a trailing 0
# when meet 25, 125, 625, ..., it will get addtional 0.
while n / (5*i) >= 1:
sums += n / (5*i)
i *= 5
return sums

"""
0
5
7
10
25
"""
19 changes: 19 additions & 0 deletions Math/179_LargestNumber.py
@@ -0,0 +1,19 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-


def comp(a, b):
return int(a + b > b + a) * 2 - 1


class Solution(object):
def largestNumber(self, nums):
nums = map(str, nums)
nums.sort(cmp=comp, reverse=True)
return str(int("".join(nums)))

"""
[1]
[1,2,3,21]
[1,2,3,23]
"""
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -60,6 +60,7 @@
* 124. [Binary Tree Maximum Path Sum](Tree/124_BinaryTreeMaximumPathSum.py)
* 144. [Binary Tree Preorder Traversal](Tree/144_BinaryTreePreorderTraversal.py)
* 145. [Binary Tree Postorder Traversal](Tree/145_BinaryTreePostorderTraversal.py)
* 173. [Binary Search Tree Iterator](Tree/173_BinarySearchTreeIterator.py)

# [Hash Table](HashTable/)

Expand Down Expand Up @@ -132,6 +133,7 @@
* 135. [Candy](DynamicProgramming/135_Candy.py)
* 139. [Word Break](DynamicProgramming/139_WordBreak.py)
* 152. [Maximum Product Subarray](working/152_MaximumProductSubarray.py)
* 174. [Dungeon Game](DynamicProgramming/174_DungeonGame.py)

# [Greedy](Greedy/)

Expand Down Expand Up @@ -173,6 +175,8 @@
* 166. [Fraction to Recurring Decimal](Math/166_FractionToRecurringDecimal.py)
* 168. [Excel Sheet Column Title](Math/168_ExcelSheetColumnTitle.py)
* 171. [Excel Sheet Column Number](Math/171_ExcelSheetColumnNumber.py)
* 172. [Factorial Trailing Zeroes](Math/172_FactorialTrailingZeroes.py)
* 179. [Largest Number](Math/179_LargestNumber.py)

# [Bit Manipulation](BitManipulation/)

Expand Down
41 changes: 41 additions & 0 deletions Tree/173_BinarySearchTreeIterator.py
@@ -0,0 +1,41 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Definition for a binary tree node
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


class BSTIterator(object):
def __init__(self, root):
self.root = root
self.node_stack = []
self.cur_node = root

def hasNext(self):
if self.cur_node or self.node_stack:
return True
else:
return False

def next(self):
# inorder traversal
while self.cur_node:
self.node_stack.append(self.cur_node)
self.cur_node = self.cur_node.left

top = self.node_stack.pop()
self.cur_node = top.right
return top.val

# Your BSTIterator will be called like this:
# i, v = BSTIterator(root), []
# while i.hasNext(): v.append(i.next())

"""
[]
[1]
[10,8,16,2,9,15,17]
"""

0 comments on commit af3c205

Please sign in to comment.