Skip to content

Commit 2a00678

Browse files
author
Chris Wu
committed
no message
1 parent 71dc13f commit 2a00678

5 files changed

+122
-1
lines changed

problems/binary-search.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,29 @@ def search(self, nums, target):
4242
#check if target in `nums`
4343
if nums[i]==target: return i
4444
else: return -1
45+
46+
47+
# 2020/7/19
48+
class Solution(object):
49+
def search(self, nums, target):
50+
if not nums: return -1
51+
52+
l = 0
53+
r = len(nums)-1
54+
55+
while True:
56+
if l>r: break
57+
if target<nums[l]: return -1
58+
if nums[r]<target: return -1
59+
60+
if target==nums[l]: return l
61+
if target==nums[r]: return r
62+
63+
m = (l+r)/2
64+
if target==nums[m]:
65+
return m
66+
elif target<nums[m]:
67+
r = m-1
68+
else:
69+
l = m+1
70+
return -1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def distributeCoins(self, root):
3+
def count_moves(node):
4+
if not node: return 0
5+
l, r = count_moves(node.left), count_moves(node.right)
6+
self.moves += abs(l)+abs(r)
7+
return node.val-1+l+r
8+
9+
self.moves = 0
10+
count_moves(root)
11+
return self.moves
12+
13+
"""
14+
If the leaf of a tree has 0 coins (an excess of -1 from what it needs), then we should push a coin from its parent onto the leaf.
15+
If it has say, 4 coins (an excess of 3), then we should push 3 coins off the leaf.
16+
In total, the number of moves from that leaf to or from its parent is excess = Math.abs(num_coins - 1).
17+
Afterwards, we never have to consider this leaf again in the rest of our calculation.
18+
19+
`count_moves(node)` count the node.left and node.right moves and return the excess number of coins in the subtree at or below this node.
20+
"""

problems/find-first-and-last-position-of-element-in-sorted-array.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,45 @@ def searchRange(self, nums, target):
4848
if nums[l]!=target or nums[r-1]!=target: return [-1, -1] #check if target in `nums`
4949
return [l, r-1]
5050

51+
52+
#2020/7/19
53+
class Solution(object):
54+
def searchRange(self, nums, target):
55+
def find_range(i, nums):
56+
start = end = i
57+
while 0<=start-1 and nums[start-1]==nums[i]:
58+
start -= 1
59+
while end+1<len(nums) and nums[end+1]==nums[i]:
60+
end += 1
61+
return [start, end]
62+
63+
64+
if not nums: return [-1, -1]
65+
l = 0
66+
r = len(nums)-1
67+
68+
while True:
69+
if l>r: break
70+
if target<nums[l]: return [-1, -1]
71+
if target>nums[r]: return [-1, -1]
72+
73+
if target==nums[l]: return find_range(l, nums)
74+
if target==nums[r]: return find_range(r, nums)
75+
76+
m = (l+r)/2
77+
if target==nums[m]:
78+
return find_range(m, nums)
79+
elif target<nums[m]:
80+
r = m-1
81+
else:
82+
l = m+1
83+
84+
return [-1, -1]
85+
86+
87+
88+
89+
90+
91+
92+

problems/house-robber-iii.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ def get_max_value(node):
3030

3131
return rob, not_rob
3232

33-
return max(get_max_value(root))
33+
return max(get_max_value(root))
34+

problems/search-insert-position.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,35 @@ def searchInsert(self, nums, target):
4747
return l
4848
else:
4949
return l+1
50+
51+
52+
#2020/7/19
53+
class Solution(object):
54+
def searchInsert(self, nums, target):
55+
if not nums: return 0
56+
57+
l = 0
58+
r = len(nums)-1
59+
60+
while True:
61+
if l>r: break
62+
if target<nums[l]: return l
63+
if target>nums[r]: return r+1
64+
65+
if target==nums[l]: return l
66+
if target==nums[r]: return r
67+
68+
m = int((l+r)/2)
69+
70+
if target==nums[m]:
71+
return m
72+
elif target>nums[m]:
73+
l = m+1
74+
else:
75+
r = m-1
76+
return 0
77+
78+
79+
80+
81+

0 commit comments

Comments
 (0)