862-Shortest-Subarray-With-Sum-At-Least-K

Sat 17 May 2025

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def shortestSubarray(self, A: List[int], K: int) -> int:
    n = len(A)
    ans = n + 1
    q = deque()
    prefix = [0] + list(itertools.accumulate(A))

    for i in range(n + 1 …

Category: leetcode

Read More

866-Prime-Palindrome

Sat 17 May 2025

https://leetcode.com/problems/prime-palindrome

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def primePalindrome(self, N: int) -> int:
    def getPalindromes(n: int) -> int:
      length = n // 2
      for i in range(10**(length - 1), 10**length):
        s = str(i)
        for j in …

Category: leetcode

Read More

867-Transpose-Matrix

Sat 17 May 2025

https://leetcode.com/problems/transpose-matrix

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def transpose(self, A: List[List[int]]) -> List[List[int]]:
    ans = [[0] * len(A) for _ in range(len(A[0]))]

    for i in range(len(A)):
      for j …

Category: leetcode

Read More

868-Binary-Gap

Sat 17 May 2025

https://leetcode.com/problems/binary-gap

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def binaryGap(self, n: int) -> int:
    ans = 0
    d = -32  # Distance between any two 1's, initialized to a reasonable small value

    while n:
      if n & 1:
        ans = max …

Category: leetcode

Read More

869-Reordered-Power-Of-2

Sat 17 May 2025

https://leetcode.com/problems/reordered-power-of-2

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def reorderedPowerOf2(self, N: int) -> bool:
    count = Counter(str(N))
    return any(Counter(str(1 << i)) == count for i in range(30))
new Solution().reorderedPowerOf2()

Score: 5

Category: leetcode

Read More

87-Scramble-String

Sat 17 May 2025

https://leetcode.com/problems/scramble-string

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isScramble(self, s1: str, s2: str) -> bool:
    if s1 == s2:
      return True
    if len(s1) != len(s2):
      return False
    if Counter(s1) != Counter(s2):
      return False

    for i …

Category: leetcode

Read More

870-Advantage-Shuffle

Sat 17 May 2025

https://leetcode.com/problems/advantage-shuffle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
from sortedcontainers import SortedList


class Solution:
  def advantageCount(self, A: List[int], B: List[int]) -> List[int]:
    sl = SortedList(A)

    for i, b in enumerate(B):
      index = 0 if sl[-1 …

Category: leetcode

Read More

871-Minimum-Number-Of-Refueling-Stops

Sat 17 May 2025

https://leetcode.com/problems/minimum-number-of-refueling-stops

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
    # dp[i] := farthest position we can reach w / i refuels
    dp = [startFuel] + [0] * len(stations)

    for i …

Category: leetcode

Read More

872-Leaf-Similar-Trees

Sat 17 May 2025

https://leetcode.com/problems/leaf-similar-trees

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
    def dfs(root: Optional[TreeNode]) -> None:
      if not root:
        return
      if not root.left and not root.right:
        yield …

Category: leetcode

Read More

873-Length-Of-Longest-Fibonacci-Subsequence

Sat 17 May 2025

https://leetcode.com/problems/length-of-longest-fibonacci-subsequence

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def lenLongestFibSubseq(self, A: List[int]) -> int:
    n = len(A)
    ans = 0
    numToIndex = {a: i for i, a in enumerate(A)}
    dp = [[2] * n for _ in range(n …

Category: leetcode

Read More
Page 66 of 77

« Prev Next »