Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Main #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions 1046. Last Stone Weight/1046. Last Stone Weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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
24 changes: 8 additions & 16 deletions 523. Continuous Subarray Sum/523. Continuous Subarray Sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
if i - hmap[total] >= 2: return True
return False
Original file line number Diff line number Diff line change
@@ -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