371-Sum-Of-Two-Integers

Fri 14 November 2025

https://leetcode.com/problems/sum-of-two-integers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def getSum(self, a: int, b: int) -> int:
    mask = 0xFFFFFFFF
    kMax = 2000

    while b:
      a, b = (a ^ b) & mask, ((a & b) << 1) & mask

    return a if a < kMax else …

Category: leetcode

Read More

372-Super-Pow

Fri 14 November 2025

https://leetcode.com/problems/super-pow

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def superPow(self, a: int, b: List[int]) -> int:
    def powMod(x: int, y: int) -> int:
      pow = 1
      for _ in range(y):
        pow = (pow * x) % k
      return pow …

Category: leetcode

Read More

375-Guess-Number-Higher-Or-Lower-Ii

Fri 14 November 2025

https://leetcode.com/problems/guess-number-higher-or-lower-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def getMoneyAmount(self, n: int) -> int:
    # Dp(i, j) := min money you need to guarantee a win of picking i..j
    @functools.lru_cache(None)
    def dp(i: int, j …

Category: leetcode

Read More

377-Combination-Sum-Iv

Fri 14 November 2025

https://leetcode.com/problems/combination-sum-iv

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

    def dfs(target: int) -> int:
      if target < 0:
        return 0
      if dp[target] != -1:
        return dp …

Category: leetcode

Read More

378-Kth-Smallest-Element-In-A-Sorted-Matrix

Fri 14 November 2025

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
    minHeap = []  # (matrix[i][j], i, j)

    i = 0
    while i < k and i < len(matrix):
      heapq.heappush(minHeap, (matrix …

Category: leetcode

Read More

38-Count-And-Say

Fri 14 November 2025

https://leetcode.com/problems/count-and-say

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def countAndSay(self, n: int) -> str:
    ans = '1'

    for _ in range(n - 1):
      nxt = ''
      i = 0
      while i < len(ans):
        count = 1
        while i + 1 < len(ans) and …

Category: leetcode

Read More

383-Ransom-Note

Fri 14 November 2025

https://leetcode.com/problems/ransom-note

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def canConstruct(self, ransomNote: str, magazine: str) -> bool:
    count1 = Counter(ransomNote)
    count2 = Counter(magazine)
    return all(count1[c] <= count2[c] for c in string.ascii_lowercase)
new Solution().canConstruct()

Score …

Category: leetcode

Read More

384-Shuffle-An-Array

Fri 14 November 2025

https://leetcode.com/problems/shuffle-an-array

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

  def reset(self) -> List[int]:
    """
    Resets the array to its original configuration and return it.
    """
    return self.nums

  def shuffle …

Category: leetcode

Read More

385-Mini-Parser

Fri 14 November 2025

https://leetcode.com/problems/mini-parser

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def deserialize(self, s: str) -> NestedInteger:
    if s[0] != '[':
      return NestedInteger(int(s))

    stack = []

    for i, c in enumerate(s):
      if c == '[':
        stack.append(NestedInteger())
        start = i + 1
      elif …

Category: leetcode

Read More

387-First-Unique-Character-In-A-String

Fri 14 November 2025

https://leetcode.com/problems/first-unique-character-in-a-string

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def firstUniqChar(self, s: str) -> int:
    count = Counter(s)

    for i, c in enumerate(s):
      if count[c] == 1:
        return i

    return -1
new Solution().firstUniqChar()

Score: 5

Category: leetcode

Read More
Page 31 of 77

« Prev Next »