697-Degree-Of-An-Array

Sat 17 May 2025

https://leetcode.com/problems/degree-of-an-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findShortestSubArray(self, nums: List[int]) -> int:
    ans = 0
    degree = 0
    debut = {}
    count = Counter()

    for i, num in enumerate(nums):
      debut.setdefault(num, i)
      count[num] += 1
      if count …

Category: leetcode

Read More

7-Reverse-Integer

Sat 17 May 2025

https://leetcode.com/problems/reverse-integer

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def reverse(self, x: int) -> int:
    ans = 0
    sign = -1 if x < 0 else 1
    x *= sign

    while x:
      ans = ans * 10 + x % 10
      x //= 10

    return 0 if …

Category: leetcode

Read More

70-Climbing-Stairs

Sat 17 May 2025

https://leetcode.com/problems/climbing-stairs

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def climbStairs(self, n: int) -> int:
    # dp[i] := # Of distinct ways to climb to i-th stair
    dp = [1, 1] + [0] * (n - 1)

    for i in range(2, n …

Category: leetcode

Read More

700-Search-In-A-Binary-Search-Tree

Sat 17 May 2025

https://leetcode.com/problems/search-in-a-binary-search-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
    if not root:
      return None
    if root.val == val:
      return root
    if root.val > val:
      return self.searchBST(root …

Category: leetcode

Read More

701-Insert-Into-A-Binary-Search-Tree

Sat 17 May 2025

https://leetcode.com/problems/insert-into-a-binary-search-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
    if not root:
      return TreeNode(val)
    if root.val > val:
      root.left = self.insertIntoBST(root.left, val)
    else:
      root …

Category: leetcode

Read More

702-Search-In-A-Sorted-Array-Of-Unknown-Size

Sat 17 May 2025

https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
# Class ArrayReader:
#   def get(self, index: int) -> int:

class Solution:
  def search(self, reader: 'ArrayReader …

Category: leetcode

Read More

709-To-Lower-Case

Sat 17 May 2025

https://leetcode.com/problems/to-lower-case

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def toLowerCase(self, str: str) -> str:
    return ''.join(chr(ord(c) + 32) if 'A' <= c <= 'Z' else c for c in str)
new Solution().toLowerCase()

Score: 5

Category: leetcode

Read More

71-Simplify-Path

Sat 17 May 2025

https://leetcode.com/problems/simplify-path

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def simplifyPath(self, path: str) -> str:
    stack = []

    for str in path.split('/'):
      if str in ('', '.'):
        continue
      if str == '..':
        if stack:
          stack.pop()
      else:
        stack.append(str)

    return '/' + '/'.join(stack …

Category: leetcode

Read More

710-Random-Pick-With-Blacklist

Sat 17 May 2025

https://leetcode.com/problems/random-pick-with-blacklist

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def __init__(self, N: int, blacklist: List[int]):
    self.validRange = N - len(blacklist)
    self.dict = {}

    for b in blacklist:
      self.dict[b] = -1

    for b in blacklist:
      if b …

Category: leetcode

Read More

711-Number-Of-Distinct-Islands-Ii

Sat 17 May 2025

https://leetcode.com/problems/number-of-distinct-islands-ii

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

    def dfs(i: int, j: int):
      if i < 0 or i == len(grid) or j < 0 or j == len …

Category: leetcode

Read More
Page 53 of 77

« Prev Next »