From c4510ca08c1247b9aaad57e0b0652a109988bf32 Mon Sep 17 00:00:00 2001 From: Azureki Date: Tue, 22 Jan 2019 23:16:33 +0800 Subject: [PATCH] Contest 120 --- 977. Squares of a Sorted Array.py | 11 +++++++ 978. Longest Turbulent Subarray.py | 35 ++++++++++++++++++++++ 979. Distribute Coins in Binary Tree.py | 28 +++++++++++++++++ 980. Unique Paths III.py | 40 +++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 977. Squares of a Sorted Array.py create mode 100644 978. Longest Turbulent Subarray.py create mode 100644 979. Distribute Coins in Binary Tree.py create mode 100644 980. Unique Paths III.py diff --git a/977. Squares of a Sorted Array.py b/977. Squares of a Sorted Array.py new file mode 100644 index 0000000..2dbdb0a --- /dev/null +++ b/977. Squares of a Sorted Array.py @@ -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)) diff --git a/978. Longest Turbulent Subarray.py b/978. Longest Turbulent Subarray.py new file mode 100644 index 0000000..acb53f5 --- /dev/null +++ b/978. Longest Turbulent Subarray.py @@ -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)) diff --git a/979. Distribute Coins in Binary Tree.py b/979. Distribute Coins in Binary Tree.py new file mode 100644 index 0000000..5d6095c --- /dev/null +++ b/979. Distribute Coins in Binary Tree.py @@ -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 + diff --git a/980. Unique Paths III.py b/980. Unique Paths III.py new file mode 100644 index 0000000..80176f5 --- /dev/null +++ b/980. Unique Paths III.py @@ -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))