Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
172,173,174,179
- Loading branch information
Showing
5 changed files
with
142 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]] | ||
""" |
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,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 | ||
""" |
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,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] | ||
""" |
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,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] | ||
""" |