Skip to content

127-weekly-contest-python #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions solution/1005.Maximize Sum Of Array After K Negations/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:

def largestSumAfterKNegations(self, A: List[int], K: int) -> int:
m = float('inf')
ans = 0
fu = []
for num in A:
if num < 0:
fu.append(num)
m = min(m, -num)
else:
ans += num
m = min(m, num)
if K >= len(fu):
K -= len(fu)
ans -= sum(fu)
if K % 2 == 1:
ans -= 2 * m
else:
fu.sort()
ans = ans - sum(fu[:K]) + sum(fu[K:])
return ans
13 changes: 13 additions & 0 deletions solution/1006.Clumsy Factorial/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:

def clumsy(self, N: int) -> int:
s = ''
calc = ['*', '//', '+', '-']
i = 0
while N != 1:
s = s + str(N) + calc[i]
i += 1
i %= 4
N -= 1
s += '1'
return eval(s)
40 changes: 40 additions & 0 deletions solution/1007.Minimum Domino Rotations For Equal Row/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution:

def minDominoRotations(self, A: List[int], B: List[int]) -> int:
a, b = A[0], B[0]
c, d = b, a
counta, countb = 0, 0
countc, countd = 1, 1
for ai, bi in zip(A[1:], B[1:]):
if ai == a:
pass
elif ai != a and bi == a:
counta += 1
else:
counta = -30000
if bi == b:
pass
elif bi != b and ai == b:
countb += 1
else:
countb = -30000
if ai == c:
pass
elif ai != c and bi == c:
countc += 1
else:
countc = -30000
if bi == d:
pass
elif bi != d and ai == d:
countd += 1
else:
countd = -30000
if counta < 0 and countb < 0 and countc < 0 and countd < 0:
return -1
else:
ans = 30000
for count in [counta, countb, countc, countd]:
if count >= 0:
ans = min(ans, count)
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


class Solution:

def bstFromPreorder(self, preorder: List[int]) -> TreeNode:
def buildtree(li):
if not li:
return
val = li[0]
if len(li) == 1:
root = TreeNode(val)
else:
l, r = [], []
for item in li[1:]:
if item > val:
r.append(item)
else:
l.append(item)
root = TreeNode(val)
root.left = buildtree(l)
root.right = buildtree(r)
return root
return buildtree(preorder)