Skip to content

Commit 0617143

Browse files
committed
127-weekly-contest-python
1 parent 71df305 commit 0617143

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
3+
def largestSumAfterKNegations(self, A: List[int], K: int) -> int:
4+
m = float('inf')
5+
ans = 0
6+
fu = []
7+
for num in A:
8+
if num < 0:
9+
fu.append(num)
10+
m = min(m, -num)
11+
else:
12+
ans += num
13+
m = min(m, num)
14+
if K >= len(fu):
15+
K -= len(fu)
16+
ans -= sum(fu)
17+
if K % 2 == 1:
18+
ans -= 2 * m
19+
else:
20+
fu.sort()
21+
ans = ans - sum(fu[:K]) + sum(fu[K:])
22+
return ans
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
3+
def clumsy(self, N: int) -> int:
4+
s = ''
5+
calc = ['*', '//', '+', '-']
6+
i = 0
7+
while N != 1:
8+
s = s + str(N) + calc[i]
9+
i += 1
10+
i %= 4
11+
N -= 1
12+
s += '1'
13+
return eval(s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution:
2+
3+
def minDominoRotations(self, A: List[int], B: List[int]) -> int:
4+
a, b = A[0], B[0]
5+
c, d = b, a
6+
counta, countb = 0, 0
7+
countc, countd = 1, 1
8+
for ai, bi in zip(A[1:], B[1:]):
9+
if ai == a:
10+
pass
11+
elif ai != a and bi == a:
12+
counta += 1
13+
else:
14+
counta = -30000
15+
if bi == b:
16+
pass
17+
elif bi != b and ai == b:
18+
countb += 1
19+
else:
20+
countb = -30000
21+
if ai == c:
22+
pass
23+
elif ai != c and bi == c:
24+
countc += 1
25+
else:
26+
countc = -30000
27+
if bi == d:
28+
pass
29+
elif bi != d and ai == d:
30+
countd += 1
31+
else:
32+
countd = -30000
33+
if counta < 0 and countb < 0 and countc < 0 and countd < 0:
34+
return -1
35+
else:
36+
ans = 30000
37+
for count in [counta, countb, countc, countd]:
38+
if count >= 0:
39+
ans = min(ans, count)
40+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
9+
class Solution:
10+
11+
def bstFromPreorder(self, preorder: List[int]) -> TreeNode:
12+
def buildtree(li):
13+
if not li:
14+
return
15+
val = li[0]
16+
if len(li) == 1:
17+
root = TreeNode(val)
18+
else:
19+
l, r = [], []
20+
for item in li[1:]:
21+
if item > val:
22+
r.append(item)
23+
else:
24+
l.append(item)
25+
root = TreeNode(val)
26+
root.left = buildtree(l)
27+
root.right = buildtree(r)
28+
return root
29+
return buildtree(preorder)

0 commit comments

Comments
 (0)