442-Find-All-Duplicates-In-An-Array
https://leetcode.com/problems/find-all-duplicates-in-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
ans = []
for num in nums:
nums[abs(num) - 1] *= -1
if nums[abs(num) - 1] > 0:
ans.append(abs(num))
return …
Read More
443-String-Compression
https://leetcode.com/problems/string-compression
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def compress(self, chars: List[str]) -> int:
ans = 0
i = 0
while i < len(chars):
letter = chars[i]
count = 0
while i < len(chars) and chars[i] == letter:
count …
Read More
444-Sequence-Reconstruction
https://leetcode.com/problems/sequence-reconstruction
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sequenceReconstruction(self, org: List[int], seqs: List[List[int]]) -> bool:
if not seqs:
return False
n = len(org)
graph = [[] for _ in range(n)]
inDegree = [0] * n
# Build …
Read More
445-Add-Two-Numbers-Ii
https://leetcode.com/problems/add-two-numbers-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
stack1 = []
stack2 = []
while l1:
stack1.append(l1)
l1 = l1.next
while l2:
stack2.append(l2)
l2 = l2.next
head = None
carry …
Read More
447-Number-Of-Boomerangs
https://leetcode.com/problems/number-of-boomerangs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
ans = 0
for x1, y1 in points:
count = defaultdict(int)
for x2, y2 in points:
ans += 2 * count[(x1 - x2)**2 …
Read More
448-Find-All-Numbers-Disappeared-In-An-Array
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
for num in nums:
index = abs(num) - 1
nums[index] = -abs(nums[index])
return [i + 1 for i, num in enumerate …
Read More
45-Jump-Game-Ii
https://leetcode.com/problems/jump-game-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def jump(self, nums: List[int]) -> int:
ans = 0
end = 0
farthest = 0
# Implicit BFS
for i in range(len(nums) - 1):
farthest = max(farthest, i + nums[i])
if …
Read More
450-Delete-Node-In-A-Bst
https://leetcode.com/problems/delete-node-in-a-bst
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return None
if root.val == key:
if not root.left:
return root.right
if not root …
Read More
451-Sort-Characters-By-Frequency
https://leetcode.com/problems/sort-characters-by-frequency
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def frequencySort(self, s: str) -> str:
ans = []
bucket = [[] for _ in range(len(s) + 1)]
for c, freq in Counter(s).items():
bucket[freq].append(c)
for freq in …
Read More
452-Minimum-Number-Of-Arrows-To-Burst-Balloons
https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
ans = 0
arrowX = -math.inf
for point in sorted(points, key=lambda x: x[1]):
if point[0] > arrowX:
ans += 1 …
Read More