From aca9b50403e29260657a23a6f668a2136454ba42 Mon Sep 17 00:00:00 2001 From: Garvit244 Date: Fri, 11 May 2018 09:11:47 +0800 Subject: [PATCH] Batching question in 100 --- 03.py => 1-100q/03.py | 0 05.py => 1-100q/05.py | 0 06.py => 1-100q/06.py | 0 10.py => 1-100q/10.py | 0 11.py => 1-100q/11.py | 0 15.py => 1-100q/15.py | 0 16.py => 1-100q/16.py | 0 17.py => 1-100q/17.py | 0 18.py => 1-100q/18.py | 0 19.py => 1-100q/19.py | 0 22.py => 1-100q/22.py | 0 23.py => 1-100q/23.py | 0 24.py => 1-100q/24.py | 0 25.py => 1-100q/25.py | 0 26.py => 1-100q/26.py | 0 30.py => 1-100q/30.py | 0 31.py => 1-100q/31.py | 0 32.py => 1-100q/32.py | 0 33.py => 1-100q/33.py | 0 34.py => 1-100q/34.py | 0 38.py => 1-100q/38.py | 0 39.py => 1-100q/39.py | 0 40.py => 1-100q/40.py | 0 41.py => 1-100q/41.py | 0 42.py => 1-100q/42.py | 0 44.py => 1-100q/44.py | 0 45.py => 1-100q/45.py | 2 +- 46.py => 1-100q/46.py | 0 48.py => 1-100q/48.py | 0 53.py => 1-100q/53.py | 0 54.py => 1-100q/54.py | 0 56.py => 1-100q/56.py | 0 57.py => 1-100q/57.py | 0 60.py => 1-100q/60.py | 0 61.py => 1-100q/61.py | 0 62.py => 1-100q/62.py | 0 63.py => 1-100q/63.py | 0 64.py => 1-100q/64.py | 0 65.py => 1-100q/65.py | 0 66.py => 1-100q/66.py | 0 67.py => 1-100q/67.py | 0 70.py => 1-100q/70.py | 0 71.py => 1-100q/71.py | 0 72.py => 1-100q/72.py | 0 73.py => 1-100q/73.py | 0 74.py => 1-100q/74.py | 0 75.py => 1-100q/75.py | 0 78.py => 1-100q/78.py | 0 79.py => 1-100q/79.py | 0 80.py => 1-100q/80.py | 0 81.py => 1-100q/81.py | 0 82.py => 1-100q/82.py | 0 83.py => 1-100q/83.py | 0 85.py => 1-100q/85.py | 0 86.py => 1-100q/86.py | 0 87.py => 1-100q/87.py | 0 90.py => 1-100q/90.py | 0 91.py => 1-100q/91.py | 0 92.py => 1-100q/92.py | 0 93.py => 1-100q/93.py | 0 1-100q/95.py | 61 +++++++++++++++++++++++++++++++++++ 1-100q/97.py | 41 +++++++++++++++++++++++ 1-100q/98.py | 42 ++++++++++++++++++++++++ TwoSum.py => 1-100q/TwoSum.py | 0 100-200q/100.py | 45 ++++++++++++++++++++++++++ 65 files changed, 190 insertions(+), 1 deletion(-) rename 03.py => 1-100q/03.py (100%) rename 05.py => 1-100q/05.py (100%) rename 06.py => 1-100q/06.py (100%) rename 10.py => 1-100q/10.py (100%) rename 11.py => 1-100q/11.py (100%) rename 15.py => 1-100q/15.py (100%) rename 16.py => 1-100q/16.py (100%) rename 17.py => 1-100q/17.py (100%) rename 18.py => 1-100q/18.py (100%) rename 19.py => 1-100q/19.py (100%) rename 22.py => 1-100q/22.py (100%) rename 23.py => 1-100q/23.py (100%) rename 24.py => 1-100q/24.py (100%) rename 25.py => 1-100q/25.py (100%) rename 26.py => 1-100q/26.py (100%) rename 30.py => 1-100q/30.py (100%) rename 31.py => 1-100q/31.py (100%) rename 32.py => 1-100q/32.py (100%) rename 33.py => 1-100q/33.py (100%) rename 34.py => 1-100q/34.py (100%) rename 38.py => 1-100q/38.py (100%) rename 39.py => 1-100q/39.py (100%) rename 40.py => 1-100q/40.py (100%) rename 41.py => 1-100q/41.py (100%) rename 42.py => 1-100q/42.py (100%) rename 44.py => 1-100q/44.py (100%) rename 45.py => 1-100q/45.py (99%) rename 46.py => 1-100q/46.py (100%) rename 48.py => 1-100q/48.py (100%) rename 53.py => 1-100q/53.py (100%) rename 54.py => 1-100q/54.py (100%) rename 56.py => 1-100q/56.py (100%) rename 57.py => 1-100q/57.py (100%) rename 60.py => 1-100q/60.py (100%) rename 61.py => 1-100q/61.py (100%) rename 62.py => 1-100q/62.py (100%) rename 63.py => 1-100q/63.py (100%) rename 64.py => 1-100q/64.py (100%) rename 65.py => 1-100q/65.py (100%) rename 66.py => 1-100q/66.py (100%) rename 67.py => 1-100q/67.py (100%) rename 70.py => 1-100q/70.py (100%) rename 71.py => 1-100q/71.py (100%) rename 72.py => 1-100q/72.py (100%) rename 73.py => 1-100q/73.py (100%) rename 74.py => 1-100q/74.py (100%) rename 75.py => 1-100q/75.py (100%) rename 78.py => 1-100q/78.py (100%) rename 79.py => 1-100q/79.py (100%) rename 80.py => 1-100q/80.py (100%) rename 81.py => 1-100q/81.py (100%) rename 82.py => 1-100q/82.py (100%) rename 83.py => 1-100q/83.py (100%) rename 85.py => 1-100q/85.py (100%) rename 86.py => 1-100q/86.py (100%) rename 87.py => 1-100q/87.py (100%) rename 90.py => 1-100q/90.py (100%) rename 91.py => 1-100q/91.py (100%) rename 92.py => 1-100q/92.py (100%) rename 93.py => 1-100q/93.py (100%) create mode 100644 1-100q/95.py create mode 100644 1-100q/97.py create mode 100644 1-100q/98.py rename TwoSum.py => 1-100q/TwoSum.py (100%) create mode 100644 100-200q/100.py diff --git a/03.py b/1-100q/03.py similarity index 100% rename from 03.py rename to 1-100q/03.py diff --git a/05.py b/1-100q/05.py similarity index 100% rename from 05.py rename to 1-100q/05.py diff --git a/06.py b/1-100q/06.py similarity index 100% rename from 06.py rename to 1-100q/06.py diff --git a/10.py b/1-100q/10.py similarity index 100% rename from 10.py rename to 1-100q/10.py diff --git a/11.py b/1-100q/11.py similarity index 100% rename from 11.py rename to 1-100q/11.py diff --git a/15.py b/1-100q/15.py similarity index 100% rename from 15.py rename to 1-100q/15.py diff --git a/16.py b/1-100q/16.py similarity index 100% rename from 16.py rename to 1-100q/16.py diff --git a/17.py b/1-100q/17.py similarity index 100% rename from 17.py rename to 1-100q/17.py diff --git a/18.py b/1-100q/18.py similarity index 100% rename from 18.py rename to 1-100q/18.py diff --git a/19.py b/1-100q/19.py similarity index 100% rename from 19.py rename to 1-100q/19.py diff --git a/22.py b/1-100q/22.py similarity index 100% rename from 22.py rename to 1-100q/22.py diff --git a/23.py b/1-100q/23.py similarity index 100% rename from 23.py rename to 1-100q/23.py diff --git a/24.py b/1-100q/24.py similarity index 100% rename from 24.py rename to 1-100q/24.py diff --git a/25.py b/1-100q/25.py similarity index 100% rename from 25.py rename to 1-100q/25.py diff --git a/26.py b/1-100q/26.py similarity index 100% rename from 26.py rename to 1-100q/26.py diff --git a/30.py b/1-100q/30.py similarity index 100% rename from 30.py rename to 1-100q/30.py diff --git a/31.py b/1-100q/31.py similarity index 100% rename from 31.py rename to 1-100q/31.py diff --git a/32.py b/1-100q/32.py similarity index 100% rename from 32.py rename to 1-100q/32.py diff --git a/33.py b/1-100q/33.py similarity index 100% rename from 33.py rename to 1-100q/33.py diff --git a/34.py b/1-100q/34.py similarity index 100% rename from 34.py rename to 1-100q/34.py diff --git a/38.py b/1-100q/38.py similarity index 100% rename from 38.py rename to 1-100q/38.py diff --git a/39.py b/1-100q/39.py similarity index 100% rename from 39.py rename to 1-100q/39.py diff --git a/40.py b/1-100q/40.py similarity index 100% rename from 40.py rename to 1-100q/40.py diff --git a/41.py b/1-100q/41.py similarity index 100% rename from 41.py rename to 1-100q/41.py diff --git a/42.py b/1-100q/42.py similarity index 100% rename from 42.py rename to 1-100q/42.py diff --git a/44.py b/1-100q/44.py similarity index 100% rename from 44.py rename to 1-100q/44.py diff --git a/45.py b/1-100q/45.py similarity index 99% rename from 45.py rename to 1-100q/45.py index 0486f68..5c48f3f 100644 --- a/45.py +++ b/1-100q/45.py @@ -1,4 +1,4 @@ -''' + ''' Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. diff --git a/46.py b/1-100q/46.py similarity index 100% rename from 46.py rename to 1-100q/46.py diff --git a/48.py b/1-100q/48.py similarity index 100% rename from 48.py rename to 1-100q/48.py diff --git a/53.py b/1-100q/53.py similarity index 100% rename from 53.py rename to 1-100q/53.py diff --git a/54.py b/1-100q/54.py similarity index 100% rename from 54.py rename to 1-100q/54.py diff --git a/56.py b/1-100q/56.py similarity index 100% rename from 56.py rename to 1-100q/56.py diff --git a/57.py b/1-100q/57.py similarity index 100% rename from 57.py rename to 1-100q/57.py diff --git a/60.py b/1-100q/60.py similarity index 100% rename from 60.py rename to 1-100q/60.py diff --git a/61.py b/1-100q/61.py similarity index 100% rename from 61.py rename to 1-100q/61.py diff --git a/62.py b/1-100q/62.py similarity index 100% rename from 62.py rename to 1-100q/62.py diff --git a/63.py b/1-100q/63.py similarity index 100% rename from 63.py rename to 1-100q/63.py diff --git a/64.py b/1-100q/64.py similarity index 100% rename from 64.py rename to 1-100q/64.py diff --git a/65.py b/1-100q/65.py similarity index 100% rename from 65.py rename to 1-100q/65.py diff --git a/66.py b/1-100q/66.py similarity index 100% rename from 66.py rename to 1-100q/66.py diff --git a/67.py b/1-100q/67.py similarity index 100% rename from 67.py rename to 1-100q/67.py diff --git a/70.py b/1-100q/70.py similarity index 100% rename from 70.py rename to 1-100q/70.py diff --git a/71.py b/1-100q/71.py similarity index 100% rename from 71.py rename to 1-100q/71.py diff --git a/72.py b/1-100q/72.py similarity index 100% rename from 72.py rename to 1-100q/72.py diff --git a/73.py b/1-100q/73.py similarity index 100% rename from 73.py rename to 1-100q/73.py diff --git a/74.py b/1-100q/74.py similarity index 100% rename from 74.py rename to 1-100q/74.py diff --git a/75.py b/1-100q/75.py similarity index 100% rename from 75.py rename to 1-100q/75.py diff --git a/78.py b/1-100q/78.py similarity index 100% rename from 78.py rename to 1-100q/78.py diff --git a/79.py b/1-100q/79.py similarity index 100% rename from 79.py rename to 1-100q/79.py diff --git a/80.py b/1-100q/80.py similarity index 100% rename from 80.py rename to 1-100q/80.py diff --git a/81.py b/1-100q/81.py similarity index 100% rename from 81.py rename to 1-100q/81.py diff --git a/82.py b/1-100q/82.py similarity index 100% rename from 82.py rename to 1-100q/82.py diff --git a/83.py b/1-100q/83.py similarity index 100% rename from 83.py rename to 1-100q/83.py diff --git a/85.py b/1-100q/85.py similarity index 100% rename from 85.py rename to 1-100q/85.py diff --git a/86.py b/1-100q/86.py similarity index 100% rename from 86.py rename to 1-100q/86.py diff --git a/87.py b/1-100q/87.py similarity index 100% rename from 87.py rename to 1-100q/87.py diff --git a/90.py b/1-100q/90.py similarity index 100% rename from 90.py rename to 1-100q/90.py diff --git a/91.py b/1-100q/91.py similarity index 100% rename from 91.py rename to 1-100q/91.py diff --git a/92.py b/1-100q/92.py similarity index 100% rename from 92.py rename to 1-100q/92.py diff --git a/93.py b/1-100q/93.py similarity index 100% rename from 93.py rename to 1-100q/93.py diff --git a/1-100q/95.py b/1-100q/95.py new file mode 100644 index 0000000..7493269 --- /dev/null +++ b/1-100q/95.py @@ -0,0 +1,61 @@ +''' + Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n. + + Example: + + Input: 3 + Output: + [ + [1,null,3,2], + [3,2,null,1], + [3,1,null,null,2], + [2,1,3], + [1,null,2,null,3] + ] + Explanation: + The above output corresponds to the 5 unique BST's shown below: + + 1 3 3 2 1 + \ / / / \ \ + 3 2 1 1 3 2 + / / \ \ + 2 1 2 3 +''' + +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def generateTrees(self, n): + """ + :type n: int + :rtype: List[TreeNode] + """ + if n == 0: + return [] + + + def generate(start, end): + result = [] + if start > end: + result.append(None) + return result + + for index in range(start, end+1): + left = generate(start, index-1) + right = generate(index+1, end) + + for l in left: + for r in right: + current = TreeNode(index) + current.left = l + current.right = r + result.append(current) + + return result + + return generate(1, n) \ No newline at end of file diff --git a/1-100q/97.py b/1-100q/97.py new file mode 100644 index 0000000..00d6fde --- /dev/null +++ b/1-100q/97.py @@ -0,0 +1,41 @@ +''' + Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. + + Example 1: + + Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" + Output: true + Example 2: + + Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" + Output: false +''' + +class Solution(object): + def isInterleave(self, s1, s2, s3): + """ + :type s1: str + :type s2: str + :type s3: str + :rtype: bool + """ + + if len(s3) != len(s1) + len(s2): + return False + + dp = [[False for _ in range(len(s2)+1)] for _ in range(len(s1)+1)] + for row in range(len(s1)+1): + for col in range(len(s2)+1): + if row == 0 and col == 0: + dp[row][col] = True + elif row == 0: + dp[row][col] =dp[row][col-1] and s2[col-1] == s3[row+col-1] + elif col == 0: + dp[row][col] = dp[row-1][col] and s1[row-1] == s3[row+col-1] + else: + dp[row][col] = (dp[row][col-1] and s2[col-1] == s3[row+col-1]) or (dp[row-1][col] and s1[row-1] == s3[row+col-1]) + + return dp[len(s1)][len(s2)] + +# Time: O(m*n) +# Space: O(m*n) \ No newline at end of file diff --git a/1-100q/98.py b/1-100q/98.py new file mode 100644 index 0000000..3bdf9aa --- /dev/null +++ b/1-100q/98.py @@ -0,0 +1,42 @@ +''' + Given a binary tree, determine if it is a valid binary search tree (BST). + + Assume a BST is defined as follows: + + The left subtree of a node contains only nodes with keys less than the node's key. + The right subtree of a node contains only nodes with keys greater than the node's key. + Both the left and right subtrees must also be binary search trees. +''' + +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def isValidBST(self, root): + """ + :type root: TreeNode + :rtype: bool + """ + if not root: + return True + + stack, result = [], [] + while stack or root: + if root: + stack.append(root) + root = root.left + else: + root = stack.pop() + result.append(root.val) + root = root.right + + previous = result[0] + for index in range(1, len(result)): + if previous >= result[index]: + return False + previous = result[index] + return True \ No newline at end of file diff --git a/TwoSum.py b/1-100q/TwoSum.py similarity index 100% rename from TwoSum.py rename to 1-100q/TwoSum.py diff --git a/100-200q/100.py b/100-200q/100.py new file mode 100644 index 0000000..2e707c7 --- /dev/null +++ b/100-200q/100.py @@ -0,0 +1,45 @@ +''' + Given two binary trees, write a function to check if they are the same or not. + + Two binary trees are considered the same if they are structurally identical and the nodes have the same value. + + Example 1: + + Input: 1 1 + / \ / \ + 2 3 2 3 + + [1,2,3], [1,2,3] + + Output: true +''' + +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def isSameTree(self, p, q): + """ + :type p: TreeNode + :type q: TreeNode + :rtype: bool + """ + if not p and not q: + return True + + stack = [(p, q)] + + while stack: + node1, node2 = stack.pop() + if node1 and node2 and node1.val == node2.val: + stack.append((node1.left, node2.left)) + stack.append((node1.right, node2.right)) + else: + if not node1 == node2: + return False + + return True \ No newline at end of file