599-Minimum-Index-Sum-Of-Two-Lists

Sat 17 May 2025

https://leetcode.com/problems/minimum-index-sum-of-two-lists

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

6-Zigzag-Conversion

Sat 17 May 2025

https://leetcode.com/problems/zigzag-conversion

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

60-Permutation-Sequence

Sat 17 May 2025

https://leetcode.com/problems/permutation-sequence

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

605-Can-Place-Flowers

Sat 17 May 2025

https://leetcode.com/problems/can-place-flowers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

606-Construct-String-From-Binary-Tree

Sat 17 May 2025

https://leetcode.com/problems/construct-string-from-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

609-Find-Duplicate-File-In-System

Sat 17 May 2025

https://leetcode.com/problems/find-duplicate-file-in-system

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

61-Rotate-List

Sat 17 May 2025

https://leetcode.com/problems/rotate-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

611-Valid-Triangle-Number

Sat 17 May 2025

https://leetcode.com/problems/valid-triangle-number

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

616-Add-Bold-Tag-In-String

Sat 17 May 2025

https://leetcode.com/problems/add-bold-tag-in-string

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

617-Merge-Two-Binary-Trees

Sat 17 May 2025

https://leetcode.com/problems/merge-two-binary-trees

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More
Page 46 of 77

« Prev Next »