forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
246 additions
and
48 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
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,47 @@ | ||
""" | ||
Given a string that contains only digits 0-9 and a target value, | ||
return all possibilities to add binary operators (not unary) +, -, or * | ||
between the digits so they prevuate to the target value. | ||
Examples: | ||
"123", 6 -> ["1+2+3", "1*2*3"] | ||
"232", 8 -> ["2*3+2", "2+3*2"] | ||
"105", 5 -> ["1*0+5","10-5"] | ||
"00", 0 -> ["0+0", "0-0", "0*0"] | ||
"3456237490", 9191 -> [] | ||
""" | ||
|
||
def add_operator(num, target): | ||
""" | ||
:type num: str | ||
:type target: int | ||
:rtype: List[str] | ||
""" | ||
res = [] | ||
if not num: return res | ||
helper(res, "", num, target, 0, 0, 0) | ||
return res | ||
|
||
def helper(res, path, num, target, pos, prev, multed): | ||
print(res, path, num, target, pos, prev, multed) | ||
if pos == len(num): | ||
if (target == prev): | ||
res.append(path) | ||
return | ||
for i in range(pos, len(num)): | ||
if i != pos and num[pos] == '0': break | ||
cur = int(num[pos:i+1]) | ||
print(cur) | ||
if (pos == 0): | ||
helper(res, path + str(cur), num, target, i+1, cur, cur) | ||
else: | ||
helper(res, path + "+" + str(cur), num, target, i+1, prev + cur, cur) | ||
helper(res, path + "-" + str(cur), num, target, i+1, prev - cur, -cur) | ||
helper(res, path + "*" + str(cur), num, target, i+1, prev - multed + multed * cur, multed * cur) | ||
|
||
|
||
# "123", 6 -> ["1+2+3", "1*2*3"] | ||
s = "123" | ||
target = 6 | ||
print(add_operator(s, target)) | ||
|
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,66 @@ | ||
""" | ||
A city's skyline is the outer contour of the silhouette formed by all the buildings | ||
in that city when viewed from a distance. | ||
Now suppose you are given the locations and height of all the buildings | ||
as shown on a cityscape photo (Figure A), | ||
write a program to output the skyline formed by these buildings collectively (Figure B). | ||
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], | ||
where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, | ||
and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. | ||
You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0. | ||
For instance, the dimensions of all buildings in Figure A are recorded as: | ||
[ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] . | ||
The output is a list of "key points" (red dots in Figure B) in the format of | ||
[ [x1,y1], [x2, y2], [x3, y3], ... ] | ||
that uniquely defines a skyline. | ||
A key point is the left endpoint of a horizontal line segment. Note that the last key point, | ||
where the rightmost building ends, | ||
is merely used to mark the termination of the skyline, and always has zero height. | ||
Also, the ground in between any two adjacent buildings should be considered part of the skyline contour. | ||
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]. | ||
Notes: | ||
The number of buildings in any input list is guaranteed to be in the range [0, 10000]. | ||
The input list is already sorted in ascending order by the left x position Li. | ||
The output list must be sorted by the x position. | ||
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, | ||
[...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged | ||
into one in the final output as such: [...[2 3], [4 5], [12 7], ...] | ||
""" | ||
|
||
import heapq | ||
|
||
def get_skyline(LRH): | ||
""" | ||
Wortst Time Complexity: O(NlogN) | ||
:type buildings: List[List[int]] | ||
:rtype: List[List[int]] | ||
""" | ||
skyline, live = [], [] | ||
i, n = 0, len(LRH) | ||
while i < n or live: | ||
if not live or i < n and LRH[i][0] <= -live[0][1]: | ||
x = LRH[i][0] | ||
while i < n and LRH[i][0] == x: | ||
heapq.heappush(live, (-LRH[i][2], -LRH[i][1])) | ||
i += 1 | ||
else: | ||
x = -live[0][1] | ||
while live and -live[0][1] <= x: | ||
heapq.heappop(live) | ||
height = len(live) and -live[0][0] | ||
if not skyline or height != skyline[-1][1]: | ||
skyline += [x, height], | ||
return skyline | ||
|
||
buildings = [ [2, 9, 10], [3, 7, 15], [5, 12, 12], [15, 20, 10], [19, 24, 8] ] | ||
# [ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ] | ||
print(get_skyline(buildings)) | ||
|
||
|
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
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,37 @@ | ||
""" | ||
Implement a trie with insert, search, and startsWith methods. | ||
Note: | ||
You may assume that all inputs are consist of lowercase letters a-z. | ||
""" | ||
class TrieNode: | ||
def __init__(self): | ||
self.children = collections.defaultdict(TrieNode) | ||
self.is_word = False | ||
|
||
class Trie: | ||
def __init__(self): | ||
self.root = TrieNode() | ||
|
||
def insert(self, word): | ||
current = self.root | ||
for letter in word: | ||
current = current.children[letter] | ||
current.is_word = True | ||
|
||
def search(self, word): | ||
current = self.root | ||
for letter in word: | ||
current = current.children.get(letter) | ||
if current is None: | ||
return False | ||
return current.is_word | ||
|
||
def startsWith(self, prefix): | ||
current = self.root | ||
for letter in prefix: | ||
current = current.children.get(letter) | ||
if current is None: | ||
return False | ||
return True | ||
|