Skip to content

Commit c7f7ca1

Browse files
committed
Leetcode submissions
1 parent 75cdcb3 commit c7f7ca1

File tree

273 files changed

+3980
-114
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

273 files changed

+3980
-114
lines changed

01-matrix.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import heapq
2+
3+
class Solution:
4+
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
5+
dist = [[0 if cell == 0 else float('inf') for cell in row] for row in mat]
6+
heap = []
7+
deleted = {}
8+
m = len(mat)
9+
n = len(mat[0])
10+
for i in range(m):
11+
for j in range(n):
12+
heapq.heappush(heap, (dist[i][j], i, j))
13+
while len(heap) > 0:
14+
currdist, i, j = heapq.heappop(heap)
15+
while (currdist, i, j) in deleted and len(heap) > 0:
16+
deleted[(currdist, i, j)] -= 1
17+
if deleted[(currdist, i, j)] == 0:
18+
del deleted[(currdist, i, j)]
19+
currdist, i, j = heapq.heappop(heap)
20+
if i > 0 and dist[i - 1][j] > 1 + currdist:
21+
deleted[(dist[i - 1][j], i - 1, j)] = deleted.get((dist[i - 1][j], i - 1, j), 0) + 1
22+
dist[i - 1][j] = 1 + currdist
23+
heapq.heappush(heap, (1 + currdist, i - 1, j))
24+
if i < m - 1 and dist[i + 1][j] > 1 + currdist:
25+
deleted[(dist[i + 1][j], i + 1, j)] = deleted.get((dist[i + 1][j], i + 1, j), 0) + 1
26+
dist[i + 1][j] = 1 + currdist
27+
heapq.heappush(heap, (1 + currdist, i + 1, j))
28+
if j > 0 and dist[i][j - 1] > 1 + currdist:
29+
deleted[(dist[i][j - 1], i, j - 1)] = deleted.get((dist[i][j - 1], i, j - 1), 0) + 1
30+
dist[i][j - 1] = 1 + currdist
31+
heapq.heappush(heap, (1 + currdist, i, j - 1))
32+
if j < n - 1 and dist[i][j + 1] > 1 + currdist:
33+
deleted[(dist[i][j + 1], i, j + 1)] = deleted.get((dist[i][j + 1], i, j + 1), 0) + 1
34+
dist[i][j + 1] = 1 + currdist
35+
heapq.heappush(heap, (1 + currdist, i, j + 1))
36+
return dist

1-bit-and-2-bit-characters.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def isValid(self, bits, i):
3+
if i == 0:
4+
if bits[0] == 0:
5+
return True
6+
else:
7+
return False
8+
if i == 1:
9+
if bits[0] == 1:
10+
return True
11+
elif bits[1] == 0:
12+
return True
13+
else:
14+
return False
15+
if bits[i - 1] == 1 and self.isValid(bits, i - 2):
16+
return True
17+
if bits[i] == 0 and self.isValid(bits, i - 1):
18+
return True
19+
return False
20+
21+
def isOneBitCharacter(self, bits: List[int]) -> bool:
22+
if len(bits) >= 2:
23+
return self.isValid(bits, len(bits) - 2)
24+
else:
25+
return True

3sum.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
n = len(nums)
4+
if n < 3:
5+
return []
6+
triplets = set()
7+
nums.sort()
8+
for i in range(n - 2):
9+
a = nums[i]
10+
start = i + 1
11+
end = n - 1
12+
while start < end:
13+
b = nums[start]
14+
c = nums[end]
15+
total = a + b + c
16+
if total == 0:
17+
triplets.add(tuple(sorted([a, b, c])))
18+
start += 1
19+
end -= 1
20+
elif total > 0:
21+
end -= 1
22+
else:
23+
start += 1
24+
return [list(triplet) for triplet in triplets]
25+
26+

add-digits.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Solution:
22
def addDigits(self, num: int) -> int:
3-
k = num % 9
4-
if k == 0:
5-
if num == 0:
6-
return 0
3+
if num == 0:
4+
return 0
5+
if num % 9 == 0:
76
return 9
8-
else:
9-
return k
7+
return num % 9

add-to-array-form-of-integer.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<int> addToArrayForm(vector<int>& A, int K) {
4+
int digit;
5+
int carry = 0;
6+
int tmp;
7+
int i = 0;
8+
int n = A.size();
9+
int nd = K>0?floor(log10(K) + 1):1;
10+
while (i != max(n,nd)) {
11+
digit = K%10;
12+
if (i>=n) A.insert(A.begin(),0);
13+
n = A.size();
14+
tmp = A[n-i-1]+digit+carry;
15+
A[n-i-1] = tmp%10;
16+
carry = tmp/10;
17+
K = K/10;
18+
i++;
19+
}
20+
if (carry == 1) {
21+
A.insert(A.begin(),0);
22+
A[0] = carry;
23+
}
24+
return A;
25+
}
26+
};

array-partition-i.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def arrayPairSum(self, nums: List[int]) -> int:
3+
nums.sort()
4+
n = len(nums)
5+
total = 0
6+
for i in range(0, n, 2):
7+
total += min(nums[i:i + 2])
8+
return total

available-captures-for-rook.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def numRookCaptures(self, board: List[List[str]]) -> int:
3+
for i in range(8):
4+
for j in range(8):
5+
if board[i][j] == 'R':
6+
ctr = 0
7+
for di, dj in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
8+
n = 0
9+
while i + n * di >= 0 and i + n * di < 8 and j + n * dj >= 0 and j + n * dj < 8:
10+
if board[i + n * di][j + n * dj] == 'B':
11+
break
12+
if board[i + n * di][j + n * dj] == 'p':
13+
ctr += 1
14+
break
15+
n += 1
16+
return ctr

backspace-string-compare.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def backspaceCompare(self, s: str, t: str) -> bool:
3+
ss = ""
4+
tt = ""
5+
for c in s:
6+
if c == "#":
7+
ss = ss[:-1]
8+
else:
9+
ss += c
10+
for c in t:
11+
if c == "#":
12+
tt = tt[:-1]
13+
else:
14+
tt += c
15+
return ss == tt
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def hasAlternatingBits(self, n: int) -> bool:
3+
bi = "{:b}".format(n)
4+
for i, c in enumerate(bi):
5+
if i > 0:
6+
if c == bi[i - 1]:
7+
return False
8+
return True

binary-search-tree-iterator.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class BSTIterator:
8+
9+
def __init__(self, root: Optional[TreeNode]):
10+
self.list = []
11+
self.inorder(root)
12+
self.i = -1
13+
14+
def inorder(self, root):
15+
if root:
16+
self.inorder(root.left)
17+
self.list.append(root.val)
18+
self.inorder(root.right)
19+
20+
def next(self) -> int:
21+
self.i += 1
22+
return self.list[self.i]
23+
24+
def hasNext(self) -> bool:
25+
return self.i < len(self.list) - 1
26+
27+
28+
# Your BSTIterator object will be instantiated and called as such:
29+
# obj = BSTIterator(root)
30+
# param_1 = obj.next()
31+
# param_2 = obj.hasNext()

binary-tree-inorder-traversal.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
9+
if not root:
10+
return []
11+
left = self.inorderTraversal(root.left)
12+
right = self.inorderTraversal(root.right)
13+
return left + [root.val] + right

binary-tree-level-order-traversal.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
9+
if not root:
10+
return []
11+
queue = [(root, 0)]
12+
levels = {}
13+
level = 0
14+
while len(queue) > 0:
15+
curr, level = queue.pop(0)
16+
levels[level] = levels.get(level, []) + [curr.val]
17+
if curr.left:
18+
queue.append((curr.left, level + 1))
19+
if curr.right:
20+
queue.append((curr.right, level + 1))
21+
return [levels[l] for l in range(level + 1)]

binary-tree-postorder-traversal.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
9+
if not root:
10+
return []
11+
left = self.postorderTraversal(root.left)
12+
right = self.postorderTraversal(root.right)
13+
return left + right + [root.val]

binary-tree-preorder-traversal.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
9+
if not root:
10+
return []
11+
left = self.preorderTraversal(root.left)
12+
right = self.preorderTraversal(root.right)
13+
return [root.val] + left + right

binary-tree-pruning.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def hasOne(self, root):
9+
if root:
10+
if root.val == 1:
11+
return True
12+
if self.hasOne(root.left) or self.hasOne(root.right):
13+
return True
14+
return False
15+
16+
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
17+
if not self.hasOne(root):
18+
return None
19+
root.left = self.pruneTree(root.left)
20+
root.right = self.pruneTree(root.right)
21+
return root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def kLengthApart(self, nums: List[int], k: int) -> bool:
3+
minDist = float('inf')
4+
prev = None
5+
numOnes = 0
6+
for i, num in enumerate(nums):
7+
if num == 1:
8+
numOnes += 1
9+
if prev:
10+
minDist = min(minDist, i - prev - 1)
11+
prev = i
12+
if numOnes <= 1:
13+
return True
14+
if minDist == float('inf'):
15+
return False
16+
return minDist >= k
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def checkString(self, s: str) -> bool:
3+
a = []
4+
b = []
5+
for i, c in enumerate(s):
6+
if c == 'a':
7+
a.append(i)
8+
if c == 'b':
9+
b.append(i)
10+
for i in a:
11+
for j in b:
12+
if i > j:
13+
return False
14+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def areOccurrencesEqual(self, s: str) -> bool:
3+
ctr = {}
4+
for c in s:
5+
ctr[c] = ctr.get(c, 0) + 1
6+
return len(set(ctr.values())) == 1
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def check(self, nums: List[int]) -> bool:
3+
n = len(nums)
4+
pivot = 0
5+
for i in range(1, n):
6+
if nums[i] < nums[i - 1]:
7+
pivot = i
8+
break
9+
for i in range(pivot, pivot + n - 1):
10+
if nums[(i + 1) % n] < nums[i % n]:
11+
return False
12+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def checkOnesSegment(self, s: str) -> bool:
3+
s += "0"
4+
segments = []
5+
chunk = ""
6+
for c in s:
7+
if c == '0' and len(chunk) > 0:
8+
segments.append(chunk)
9+
chunk = ""
10+
elif c == '1':
11+
chunk += '1'
12+
return len(segments) <= 1

0 commit comments

Comments
 (0)