115-Distinct-Subsequences

Sat 17 May 2025

https://leetcode.com/problems/distinct-subsequences

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numDistinct(self, s: str, t: str) -> int:
    m = len(s)
    n = len(t)
    dp = [[0] * (n + 1) for _ in range(m + 1)]

    for i in range(m …

Category: leetcode

Read More

116-Populating-Next-Right-Pointers-In-Each-Node

Sat 17 May 2025

https://leetcode.com/problems/populating-next-right-pointers-in-each-node

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
    if not root:
      return None

    def connectTwoNodes(p, q) -> None:
      if not p:
        return
      p.next = q
      connectTwoNodes(p.left, p …

Category: leetcode

Read More

117-Populating-Next-Right-Pointers-In-Each-Node-Ii

Sat 17 May 2025

https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def connect(self, root: 'Node') -> 'Node':
    node = root  # The node just above current needling

    while node:
      dummy = Node(0)  # Dummy node before needling
      # Needle children of node
      needle = dummy …

Category: leetcode

Read More

118-Pascals-Triangle

Sat 17 May 2025

https://leetcode.com/problems/pascals-triangle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def generate(self, numRows: int) -> List[List[int]]:
    ans = []

    for i in range(numRows):
      ans.append([1] * (i + 1))

    for i in range(2, numRows):
      for j in range …

Category: leetcode

Read More

119-Pascals-Triangle-Ii

Sat 17 May 2025

https://leetcode.com/problems/pascals-triangle-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def getRow(self, rowIndex: int) -> List[int]:
    ans = [1] * (rowIndex + 1)

    for i in range(2, rowIndex + 1):
      for j in range(1, i):
        ans[i - j] += ans[i …

Category: leetcode

Read More

12-Integer-To-Roman

Sat 17 May 2025

https://leetcode.com/problems/integer-to-roman

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def intToRoman(self, num: int) -> str:
    valueSymbols = [(1000, 'M'), (900, 'CM'),
                    (500, 'D'), (400, 'CD'),
                    (100, 'C'), (90, 'XC'),
                    (50, 'L'), (40, 'XL'),
                    (10, 'X'), (9, 'IX'),
                    (5, 'V …

Category: leetcode

Read More

120-Triangle

Sat 17 May 2025

https://leetcode.com/problems/triangle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minimumTotal(self, triangle: List[List[int]]) -> int:
    for i in reversed(range(len(triangle) - 1)):
      for j in range(i + 1):
        triangle[i][j] += min(triangle[i + 1 …

Category: leetcode

Read More

121-Best-Time-To-Buy-And-Sell-Stock

Sat 17 May 2025

https://leetcode.com/problems/best-time-to-buy-and-sell-stock

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxProfit(self, prices: List[int]) -> int:
    sellOne = 0
    holdOne = -math.inf

    for price in prices:
      sellOne = max(sellOne, holdOne + price)
      holdOne = max(holdOne, -price)

    return sellOne
new Solution …

Category: leetcode

Read More

122-Best-Time-To-Buy-And-Sell-Stock-Ii

Sat 17 May 2025

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxProfit(self, prices: List[int]) -> int:
    sell = 0
    hold = -math.inf

    for price in prices:
      sell = max(sell, hold + price)
      hold = max(hold, sell - price)

    return sell
new …

Category: leetcode

Read More

123-Best-Time-To-Buy-And-Sell-Stock-Iii

Sat 17 May 2025

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxProfit(self, prices: List[int]) -> int:
    sellTwo = 0
    holdTwo = -math.inf
    sellOne = 0
    holdOne = -math.inf

    for price in prices:
      sellTwo = max(sellTwo, holdTwo + price)
      holdTwo = max(holdTwo …

Category: leetcode

Read More
Page 8 of 77

« Prev Next »