From 29a10ba9b3a69d8c0fc790e0dccbbc00d3998ba5 Mon Sep 17 00:00:00 2001 From: 149ps Date: Tue, 5 Apr 2022 01:17:47 -0700 Subject: [PATCH 1/3] O(n) time and o(n) space using BFS and Deque. --- ...988. Smallest String Starting From Leaf.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 988. Smallest String Starting From Leaf/988. Smallest String Starting From Leaf.py diff --git a/988. Smallest String Starting From Leaf/988. Smallest String Starting From Leaf.py b/988. Smallest String Starting From Leaf/988. Smallest String Starting From Leaf.py new file mode 100644 index 0000000..54ebe71 --- /dev/null +++ b/988. Smallest String Starting From Leaf/988. Smallest String Starting From Leaf.py @@ -0,0 +1,54 @@ +""" +You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'. + +Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root. + +As a reminder, any shorter prefix of a string is lexicographically smaller. + +For example, "ab" is lexicographically smaller than "aba". +A leaf of a node is a node that has no children. + + + +Example 1: + + +Input: root = [0,1,2,3,4,3,4] +Output: "dba" +Example 2: + + +Input: root = [25,1,3,1,3,0,2] +Output: "adz" +Example 3: + + +Input: root = [2,2,1,null,1,0,null,0] +Output: "abc" + + +Constraints: + +The number of nodes in the tree is in the range [1, 8500]. +0 <= Node.val <= 25 +""" +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def smallestFromLeaf(self, root: Optional[TreeNode]) -> str: + q,result = collections.deque(),None + q.append((root,chr(root.val+97))) + while q: + node,path = q.popleft() + if not node.left and not node.right: + if not result: + result = path + else: + result = min(result,path) + if node.left: q.append((node.left,chr(node.left.val+97)+path)) + if node.right: q.append((node.right,chr(node.right.val+97)+path)) + return result \ No newline at end of file From dc8a236b41645575db9b09da7a7b3eeef5af053d Mon Sep 17 00:00:00 2001 From: 149ps Date: Tue, 5 Apr 2022 23:48:09 -0700 Subject: [PATCH 2/3] O(n) time and O(n) space using hashmap. --- .../523. Continuous Subarray Sum.py | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/523. Continuous Subarray Sum/523. Continuous Subarray Sum.py b/523. Continuous Subarray Sum/523. Continuous Subarray Sum.py index 53800dc..0a0e925 100644 --- a/523. Continuous Subarray Sum/523. Continuous Subarray Sum.py +++ b/523. Continuous Subarray Sum/523. Continuous Subarray Sum.py @@ -22,20 +22,12 @@ """ class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: - """ - The idea here is if two sums have the same module and they are atleast two indexes apart then we can return true otherwise false. - For [0,2], k=2 prefix sum would be [0,2] and sum2 = 2 and sum1 = 0. sum2 - sum1 can be divided by k and the distance between those two indexes is atleast 2. - """ - hmap = {0:-1} # initially at index -1 the sum would be zero. - for i in range(1,len(nums)): - nums[i] += nums[i-1] # prefix sum - for i in range(len(nums)): - temp = nums[i] - if k: - temp %= k - if temp in hmap.keys(): - if i - hmap[temp] >= 2: - return True + hmap,total = {},0 + for i,num in enumerate(nums): + total = (total + num) % k + if total == 0 and i > 0: return True # if total % k =0 that means we have a multiple of k present already and check if the array size is greater than or equal to 2. + if total not in hmap: + hmap[total] = i else: - hmap[temp] = i - return False \ No newline at end of file + if i - hmap[total] >= 2: return True + return False From b8c7c0e5cbcd644241ad52902bd4877eaec1c55b Mon Sep 17 00:00:00 2001 From: 149ps Date: Wed, 6 Apr 2022 22:54:21 -0700 Subject: [PATCH 3/3] O(nlogn) time and O(n) space using max heap. --- 1046. Last Stone Weight/1046. Last Stone Weight.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/1046. Last Stone Weight/1046. Last Stone Weight.py b/1046. Last Stone Weight/1046. Last Stone Weight.py index 2c02614..8dec6e2 100644 --- a/1046. Last Stone Weight/1046. Last Stone Weight.py +++ b/1046. Last Stone Weight/1046. Last Stone Weight.py @@ -31,12 +31,8 @@ def lastStoneWeight(self, stones: List[int]) -> int: max_heap = [-val for val in stones] heapq.heapify(max_heap) while len(max_heap) > 1: - y = (-1) * heapq.heappop(max_heap) - x = (-1) * heapq.heappop(max_heap) - if x == y: - if not max_heap: # consider a case [1,1] - return 0 - continue - else: - heapq.heappush(max_heap,x-y) - return max_heap[0] if max_heap[0] > 0 else (-1)*max_heap[0] \ No newline at end of file + stone1 = (-1) * heapq.heappop(max_heap) + stone2 = (-1) * heapq.heappop(max_heap) + if stone1 != stone2: + heapq.heappush(max_heap,-(stone1-stone2)) + return -max_heap[0] if max_heap else 0 \ No newline at end of file