Skip to content

Commit

Permalink
Contest 120
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Jan 22, 2019
1 parent e4728d3 commit c4510ca
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 977. Squares of a Sorted Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def sortedSquares(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
return sorted(map(lambda x: x * x, A))


lst = [-4,-1,0,3,10]
print(Solution().sortedSquares(lst))
35 changes: 35 additions & 0 deletions 978. Longest Turbulent Subarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution:
def maxTurbulenceSize(self, A):
"""
:type A: List[int]
:rtype: int
"""
length = len(A)
comp = []
for i in range(1, length):
if A[i] > A[i - 1]:
comp.append(1)
elif A[i] < A[i - 1]:
comp.append(-1)
else:
comp.append(0)
res = 0
length = len(comp)
i = 0
while i != length:
num = 1 if comp[i] else 0
j = i + 1
while j != length:
if comp[j] and comp[j] == -comp[j - 1]:
num += 1
j += 1
else:
break

res = max(res, num)
i = j
return res + 1


A = [9,9,9]
print(Solution().maxTurbulenceSize(A))
28 changes: 28 additions & 0 deletions 979. Distribute Coins in Binary Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


class Solution:
def distributeCoins(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = 0

def dfs(root):
if not root:
return 1
left = dfs(root.left)
right = dfs(root.right)
self.res += abs(left-1)+abs(right-1)
root.val += left -1 + right -1
return root.val

dfs(root)
return self.res

40 changes: 40 additions & 0 deletions 980. Unique Paths III.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution:
def uniquePathsIII(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""

self.res = 0
dirs = [(-1, 0), (1, 0), (1, 0), (0, 1)]
left = 0
height = len(grid)
length = len(grid[0])
visited = [[0] * length for _ in range(height)]

def dfs(x, y, left):
if left == 0 and grid[x][y] == 2:
self.res += 1
return
for d in dirs:
i, j = x + d[0], y + d[1]
if (0 <= i < height and 0 <= j < length and grid[i][j] != -1
and not visited[i][j]):
visited[i][j] = 1
dfs(i, j, left - 1)
visited[i][j] = 0

for i in range(height):
for j in range(length):
if grid[i][j] == 1:
x, y = i, j
elif grid[i][j] == 0:
left += 1
visited[x][y] = 1
print(x, y, left)
dfs(x, y, left + 1)
return self.res


grid = [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 2, -1]]
print(Solution().uniquePathsIII(grid))

0 comments on commit c4510ca

Please sign in to comment.