Skip to content

Commit eda1d89

Browse files
committed
Binary Search Tree Iterator/ Dungeon Game
1 parent 119e687 commit eda1d89

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

173 Binary Search Tree Iterator.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
3+
4+
Calling next() will return the next smallest number in the BST.
5+
6+
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
7+
'''
8+
9+
# Definition for a binary tree node
10+
# class TreeNode(object):
11+
# def __init__(self, x):
12+
# self.val = x
13+
# self.left = None
14+
# self.right = None
15+
16+
class BSTIterator(object):
17+
def __init__(self, root):
18+
"""
19+
:type root: TreeNode
20+
"""
21+
self.stack = []
22+
self._pushLeft(root)
23+
24+
def hasNext(self):
25+
"""
26+
:rtype: bool
27+
"""
28+
return self.stack
29+
30+
def next(self):
31+
"""
32+
:rtype: int
33+
"""
34+
node = self.stack.pop()
35+
self._pushLeft(node.right)
36+
return node.val
37+
38+
def _pushLeft(self, node):
39+
while node:
40+
self.stack.append(node)
41+
node = node.left
42+
43+
44+
# Your BSTIterator will be called like this:
45+
# i, v = BSTIterator(root), []
46+
# while i.hasNext(): v.append(i.next())
47+
48+
49+
if __name__ == "__main__":
50+
None

174 Dungeon Game.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
3+
4+
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
5+
6+
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
7+
8+
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
9+
10+
11+
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
12+
13+
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
14+
15+
-2 (K) -3 3
16+
-5 -10 1
17+
10 30 -5 (P)
18+
19+
Notes:
20+
21+
The knight's health has no upper bound.
22+
Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
23+
'''
24+
25+
class Solution(object):
26+
def calculateMinimumHP(self, dungeon):
27+
"""
28+
:type dungeon: List[List[int]]
29+
:rtype: int
30+
"""
31+
n = len(dungeon)
32+
m = len(dungeon[0])
33+
dp = [[0 for __ in range(m)] for __ in range(n)]
34+
dp[-1][-1] = 1 if dungeon[-1][-1] > 0 else 1 - dungeon[-1][-1]
35+
for i in range(m - 2, -1, -1):
36+
dp[-1][i] = max(1, dp[-1][i + 1] - dungeon[-1][i])
37+
for j in range(n - 2, -1, -1):
38+
dp[j][-1] = max(1, dp[j + 1][-1] - dungeon[j][-1])
39+
for j in range(n - 2, -1, -1):
40+
for i in range(m - 2, -1, -1):
41+
dp[j][i] = max(min(dp[j + 1][i], dp[j][i + 1]) - dungeon[j][i], 1)
42+
return dp[0][0]
43+
44+
45+
if __name__ == "__main__":
46+
assert Solution().calculateMinimumHP([[-2, -3, 3], [-5, -10, 1], [10, 30, -5]]) == 7

0 commit comments

Comments
 (0)