Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5b009ad

Browse files
authored
Merge pull request #2 from 149ps/main
Main
2 parents 01cf981 + b8c7c0e commit 5b009ad

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

‎1046. Last Stone Weight/1046. Last Stone Weight.py‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ def lastStoneWeight(self, stones: List[int]) -> int:
3131
max_heap = [-val for val in stones]
3232
heapq.heapify(max_heap)
3333
while len(max_heap) > 1:
34-
y = (-1) * heapq.heappop(max_heap)
35-
x = (-1) * heapq.heappop(max_heap)
36-
if x == y:
37-
if not max_heap: # consider a case [1,1]
38-
return 0
39-
continue
40-
else:
41-
heapq.heappush(max_heap,x-y)
42-
return max_heap[0] if max_heap[0] > 0 else (-1)*max_heap[0]
34+
stone1 = (-1) * heapq.heappop(max_heap)
35+
stone2 = (-1) * heapq.heappop(max_heap)
36+
if stone1 != stone2:
37+
heapq.heappush(max_heap,-(stone1-stone2))
38+
return -max_heap[0] if max_heap else 0

‎523. Continuous Subarray Sum/523. Continuous Subarray Sum.py‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@
2222
"""
2323
class Solution:
2424
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
25-
"""
26-
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.
27-
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.
28-
"""
29-
hmap = {0:-1} # initially at index -1 the sum would be zero.
30-
for i in range(1,len(nums)):
31-
nums[i] += nums[i-1] # prefix sum
32-
for i in range(len(nums)):
33-
temp = nums[i]
34-
if k:
35-
temp %= k
36-
if temp in hmap.keys():
37-
if i - hmap[temp] >= 2:
38-
return True
25+
hmap,total = {},0
26+
for i,num in enumerate(nums):
27+
total = (total + num) % k
28+
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.
29+
if total not in hmap:
30+
hmap[total] = i
3931
else:
40-
hmap[temp] =i
41-
return False
32+
ifi-hmap[total] >=2: returnTrue
33+
return False
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
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'.
3+
4+
Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
5+
6+
As a reminder, any shorter prefix of a string is lexicographically smaller.
7+
8+
For example, "ab" is lexicographically smaller than "aba".
9+
A leaf of a node is a node that has no children.
10+
11+
12+
13+
Example 1:
14+
15+
16+
Input: root = [0,1,2,3,4,3,4]
17+
Output: "dba"
18+
Example 2:
19+
20+
21+
Input: root = [25,1,3,1,3,0,2]
22+
Output: "adz"
23+
Example 3:
24+
25+
26+
Input: root = [2,2,1,null,1,0,null,0]
27+
Output: "abc"
28+
29+
30+
Constraints:
31+
32+
The number of nodes in the tree is in the range [1, 8500].
33+
0 <= Node.val <= 25
34+
"""
35+
# Definition for a binary tree node.
36+
# class TreeNode:
37+
# def __init__(self, val=0, left=None, right=None):
38+
# self.val = val
39+
# self.left = left
40+
# self.right = right
41+
class Solution:
42+
def smallestFromLeaf(self, root: Optional[TreeNode]) -> str:
43+
q,result = collections.deque(),None
44+
q.append((root,chr(root.val+97)))
45+
while q:
46+
node,path = q.popleft()
47+
if not node.left and not node.right:
48+
if not result:
49+
result = path
50+
else:
51+
result = min(result,path)
52+
if node.left: q.append((node.left,chr(node.left.val+97)+path))
53+
if node.right: q.append((node.right,chr(node.right.val+97)+path))
54+
return result

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /