162-Find-Peak-Element

Sat 17 May 2025

https://leetcode.com/problems/find-peak-element

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findPeakElement(self, nums: List[int]) -> int:
    l = 0
    r = len(nums) - 1

    while l < r:
      m = (l + r) // 2
      if nums[m] >= nums[m + 1]:
        r = m
      else …

Category: leetcode

Read More

163-Missing-Ranges

Sat 17 May 2025

https://leetcode.com/problems/missing-ranges

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
    def getRange(lo: int, hi: int) -> str:
      if lo == hi:
        return str(lo)
      return str(lo) + '->' + str …

Category: leetcode

Read More

164-Maximum-Gap

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Bucket:
  def __init__(self, mini: int, maxi: int):
    self.mini = mini
    self.maxi = maxi


class Solution:
  def maximumGap(self, nums: List[int]) -> int:
    if len(nums) < 2:
      return 0

    mini …

Category: leetcode

Read More

165-Compare-Version-Numbers

Sat 17 May 2025

https://leetcode.com/problems/compare-version-numbers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def compareVersion(self, version1: str, version2: str) -> int:
    levels1 = version1.split('.')
    levels2 = version2.split('.')
    length = max(len(levels1), len(levels2))

    for i in range(length):
      v1 = int(levels1[i …

Category: leetcode

Read More

166-Fraction-To-Recurring-Decimal

Sat 17 May 2025

https://leetcode.com/problems/fraction-to-recurring-decimal

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def fractionToDecimal(self, numerator: int, denominator: int) -> str:
    if numerator == 0:
      return '0'

    ans = ''

    if (numerator < 0) ^ (denominator < 0):
      ans += '-'

    numerator = abs(numerator)
    denominator = abs(denominator)
    ans += str(numerator …

Category: leetcode

Read More

167-Two-Sum-Ii-Input-Array-Is-Sorted

Sat 17 May 2025

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def twoSum(self, numbers: List[int], target: int) -> List[int]:
    l = 0
    r = len(numbers) - 1

    while l < r:
      summ = numbers[l] + numbers[r]
      if summ == target:
        return [l …

Category: leetcode

Read More

168-Excel-Sheet-Column-Title

Sat 17 May 2025

https://leetcode.com/problems/excel-sheet-column-title

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def convertToTitle(self, n: int) -> str:
    return self.convertToTitle((n - 1) // 26) + \
        chr(ord('A') + (n - 1) % 26) if n else ''
new Solution().convertToTitle()

Score: 5

Category: leetcode

Read More

169-Majority-Element

Sat 17 May 2025

https://leetcode.com/problems/majority-element

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

    for num in nums:
      if count == 0:
        ans = num
      count += (1 if num == ans else -1)

    return ans …

Category: leetcode

Read More

17-Letter-Combinations-Of-A-Phone-Number

Sat 17 May 2025

https://leetcode.com/problems/letter-combinations-of-a-phone-number

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def letterCombinations(self, digits: str) -> List[str]:
    if not digits:
      return []

    digitToLetters = ['', '', 'abc', 'def', 'ghi',
                      'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
    ans = []

    def dfs(i: int, path: List[chr]) -> None …

Category: leetcode

Read More

171-Excel-Sheet-Column-Number

Sat 17 May 2025

https://leetcode.com/problems/excel-sheet-column-number

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

    for c in s:
      ans = ans * 26 + ord(c) - ord('@')

    return ans
new Solution().titleToNumber()

Score: 5

Category: leetcode

Read More
Page 13 of 77

« Prev Next »