From af3c205efe61c28b867a8c79cee05352c7ba5bca Mon Sep 17 00:00:00 2001 From: xuelangZF Date: Tue, 8 Dec 2015 16:22:55 +0800 Subject: [PATCH] 172,173,174,179 --- DynamicProgramming/174_DungeonGame.py | 53 +++++++++++++++++++++++++++ Math/172_FactorialTrailingZeroes.py | 25 +++++++++++++ Math/179_LargestNumber.py | 19 ++++++++++ README.md | 4 ++ Tree/173_BinarySearchTreeIterator.py | 41 +++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 DynamicProgramming/174_DungeonGame.py create mode 100755 Math/172_FactorialTrailingZeroes.py create mode 100644 Math/179_LargestNumber.py create mode 100644 Tree/173_BinarySearchTreeIterator.py diff --git a/DynamicProgramming/174_DungeonGame.py b/DynamicProgramming/174_DungeonGame.py new file mode 100644 index 0000000..ebc146f --- /dev/null +++ b/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]] +""" diff --git a/Math/172_FactorialTrailingZeroes.py b/Math/172_FactorialTrailingZeroes.py new file mode 100755 index 0000000..8332bc1 --- /dev/null +++ b/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 +""" diff --git a/Math/179_LargestNumber.py b/Math/179_LargestNumber.py new file mode 100644 index 0000000..0173b4e --- /dev/null +++ b/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] +""" diff --git a/README.md b/README.md index 2e651c7..de7152d 100644 --- a/README.md +++ b/README.md @@ -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/) @@ -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/) @@ -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/) diff --git a/Tree/173_BinarySearchTreeIterator.py b/Tree/173_BinarySearchTreeIterator.py new file mode 100644 index 0000000..b149c1b --- /dev/null +++ b/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] +"""