Skip to content

Commit 25bbcbe

Browse files
committedMar 30, 2019
Adding solution of 1008 and 1007
1 parent 5b9cd0c commit 25bbcbe

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
 

‎1000-1100q/1007.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
3+
4+
We may rotate the i-th domino, so that A[i] and B[i] swap values.
5+
6+
Return the minimum number of rotations so that all the values in A are the same, or all the values in B are the same.
7+
8+
If it cannot be done, return -1.
9+
Input: A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]
10+
Output: 2
11+
Explanation:
12+
The first figure represents the dominoes as given by A and B: before we do any rotations.
13+
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
14+
'''
15+
16+
class Solution(object):
17+
def minDominoRotations(self, A, B):
18+
"""
19+
:type A: List[int]
20+
:type B: List[int]
21+
:rtype: int
22+
"""
23+
if len(A) != len(B):
24+
return -1
25+
if len(A) == 0:
26+
return 0
27+
28+
for possibility in set([A[0], B[0]]):
29+
top_rotation, bottom_rotation =0, 0
30+
for a_num, b_num in zip(A, B):
31+
if possibility not in [a_num, b_num]:
32+
break
33+
top_rotation += int(b_num != possibility)
34+
bottom_rotation += int(a_num != possibility)
35+
else:
36+
return min(top_rotation, bottom_rotation)
37+
return -1
38+

‎1000-1100q/1008.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Return the root node of a binary search tree that matches the given preorder traversal.
3+
4+
(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.)
5+
'''
6+
7+
# Definition for a binary tree node.
8+
# class TreeNode(object):
9+
# def __init__(self, x):
10+
# self.val = x
11+
# self.left = None
12+
# self.right = None
13+
14+
class Solution(object):
15+
def bstFromPreorder(self, preorder):
16+
"""
17+
:type preorder: List[int]
18+
:rtype: TreeNode
19+
"""
20+
root = TreeNode(preorder[0])
21+
stack = [root]
22+
for index in range(1, len(preorder)):
23+
new_node = TreeNode(preorder[index])
24+
if new_node.val < stack[-1].val:
25+
stack[-1].left = new_node
26+
else:
27+
parent = None
28+
while stack and new_node.val > stack[-1].val:
29+
parent = stack.pop()
30+
parent.right = new_node
31+
stack.append(new_node)
32+
return root
33+

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1414
|1014|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/) | [Python](./1000-1100q/1014.py) | Medium|
1515
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Python](./1000-1100q/1013.py)|Easy|
1616
|1012|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [Python](./1000-1100q/1012.py)|Easy|
17+
|1008|[Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal)|[Python](./1000-1100q/1008.py)|Medium|
18+
|1007|[Minimum Domino Rotations For Equal Row](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row)|[Python](./1000-1100q/1007.py)|Medium|
1719
|1004|[Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii)|[Python](./1000-1100q/1004.py)|Medium|
1820
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions)|[Python](./1000-1100q/1003.py)|Medium|
1921
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters)|[Python](./1000-1100q/1002.py)|Easy|

0 commit comments

Comments
 (0)