599-Minimum-Index-Sum-Of-Two-Lists
https://leetcode.com/problems/minimum-index-sum-of-two-lists
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
ans = []
restaurantToIndex = {restaurant: i for i,
restaurant in enumerate(list1)}
minSum = math.inf
for i, restaurant in enumerate …
Read More
6-Zigzag-Conversion
https://leetcode.com/problems/zigzag-conversion
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def convert(self, s: str, numRows: int) -> str:
rows = [''] * numRows
k = 0
direction = (numRows == 1) - 1
for c in s:
rows[k] += c
if k == 0 or k == numRows …
Read More
60-Permutation-Sequence
https://leetcode.com/problems/permutation-sequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def getPermutation(self, n: int, k: int) -> str:
ans = ''
nums = [i + 1 for i in range(n)]
factorial = [1] * (n + 1) # factorial[i] := i!
for i in range(2 …
Read More
605-Can-Place-Flowers
https://leetcode.com/problems/can-place-flowers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
for i, flower in enumerate(flowerbed):
if flower == 0 and (i == 0 or flowerbed[i - 1] == 0) and (i == len …
Read More
606-Construct-String-From-Binary-Tree
https://leetcode.com/problems/construct-string-from-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def tree2str(self, t: Optional[TreeNode]) -> str:
def dfs(root: Optional[TreeNode]) -> str:
if not root:
return ''
if root.right:
return str(root.val) + '(' + dfs(root.left) + ')(' + dfs(root …
Read More
609-Find-Duplicate-File-In-System
https://leetcode.com/problems/find-duplicate-file-in-system
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
contentToPathFiles = defaultdict(list)
for path in paths:
words = path.split(' ')
rootPath = words[0] # "root/d1/d2/.../dm"
for fileAndContent in …
Read More
61-Rotate-List
https://leetcode.com/problems/rotate-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next or k == 0:
return head
tail = head
length = 1
while tail.next:
tail = tail …
Read More
611-Valid-Triangle-Number
https://leetcode.com/problems/valid-triangle-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def triangleNumber(self, nums: List[int]) -> int:
ans = 0
nums.sort()
for k in range(len(nums) - 1, 1, -1):
i = 0
j = k - 1
while i < j:
if …
Read More
616-Add-Bold-Tag-In-String
https://leetcode.com/problems/add-bold-tag-in-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addBoldTag(self, s: str, words: List[str]) -> str:
n = len(s)
ans = []
# bold[i] := True if s[i] should be bolded
bold = [0] * n
boldEnd = -1 # s[i …
Read More
617-Merge-Two-Binary-Trees
https://leetcode.com/problems/merge-two-binary-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:
return None
val = (root1.val if root1 else 0) + (root2.val if …
Read More